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.

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘