dsxml2169 2017-11-20 23:22 采纳率: 0%
浏览 195

Go SSH密钥不适用于crypto / ssh,但可以手动进行

I generated a SSH RSA keypair with the crypto/ssh package. However, when I try to use it via a script in Go I'm getting the error:

unable to connect: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

When I connect to the remote device via CLI, it connects successfully:

ssh -i ~/.ssh/test_key_1 username@172.22.4.1

Could I possibly be using the package incorrectly?

Note: Private key does NOT have a passphrase.

package main

import (
    "golang.org/x/crypto/ssh"
    "io/ioutil"
    "log"
)

func main() {
    privateKeyFile := "/Users/username/.ssh/test_key_1"
    remoteIP := "172.22.4.1:22"
    user := "username"

    privateKeyBytes, err := ioutil.ReadFile(privateKeyFile)
    if err != nil {
        log.Fatal(err)
    }

    key, err := ssh.ParsePrivateKey(privateKeyBytes)
    if err != nil {
        log.Fatal(err)
    }

    config := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{
            // Use the PublicKeys method for remote authentication.
            ssh.PublicKeys(key),
        },
        // using InsecureIgnoreHostKey() for testing purposes
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }

    client, err := ssh.Dial("tcp", remoteIP, config)
    if err != nil {
        log.Fatalf("unable to connect: %v", err)
    }
    defer client.Close()

    fmt.Println("Success!")
}
  • 写回答

1条回答 默认 最新

  • doumi7854 2017-11-21 18:15
    关注

    After the long process of component isolation I was finally able to verify why my Key Pairs weren't authenticating. It's due to a custom connivence package I was using that is generating slightly off Public Keys.

    I've post on an open issue he had:

    https://github.com/ianmcmahon/encoding_ssh/issues/1

    In short:

    The Public Key that was created using the EncodePublicKey() function is as below: (truncated for brevity)

    ssh-rsa AAAAB3NzaC1yc2EAAAAEAAEAAQAAAgC2u3I/nbN9jcWDV..

    However when running ssh-keygen -y -f id_rsa the below is created:

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC2u3I/nbN9jcWDV...

    Notice how the bits in bold are slightly different. This causes SSH authentication to not work.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度