dqtok88424 2017-10-12 02:11
浏览 139

使用pubkey登录golang ssh失败

In mac,I can login rain01 without password using key,In rain01 ,I can run command ssh tree01,then I logged.but using golang ssh package,I was told SSH_AUTH_SOCK is null,and this is error msg dial unix: missing address this is my code

func SSHClient(hostport string, username string) (*ssh.Client, error) {
sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
    logrus.Infof("error login,details: %s",err.Error())
    return nil,err
}

agent := agent.NewClient(sock)

signers, err := agent.Signers()
if err != nil {
    logrus.Infof("error login,details: %s",err.Error())
    return nil,err
}

auths := []ssh.AuthMethod{ssh.PublicKeys(signers...)}

cfg := &ssh.ClientConfig{
    User: username,
    Auth: auths,
    HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
        return nil
    },
}
cfg.SetDefaults()
logrus.Infof("tcp dial to %s",hostport)
client, err := ssh.Dial("tcp", hostport, cfg)
if err != nil {
    logrus.Infof("error login,details: %s",err.Error())
    return nil,err
}
return client, nil
}

Now I have the var ssh_auth_sock,but there is another problem

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

and main ssh config is

StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      %h/.ssh/authorized_keys
HostbasedAuthentication no
IgnoreRhosts yes
RhostsRSAAuthentication no
  • 写回答

1条回答

  • du67560 2019-07-25 17:49
    关注

    Try https://github.com/appleboy/easyssh-proxy package

    package main
    
    import (
        "fmt"
        "time"
    
        "github.com/appleboy/easyssh-proxy"
    )
    
    func main() {
        // Create MakeConfig instance with remote username, server address and path to private key.
        ssh := &easyssh.MakeConfig{
            User:   "appleboy",
            Server: "example.com",
            // Optional key or Password without either we try to contact your agent SOCKET
            //Password: "password",
            // Paste your source content of private key
            // Key: `-----BEGIN RSA PRIVATE KEY-----
            // MIIEpAIBAAKCAQEA4e2D/qPN08pzTac+a8ZmlP1ziJOXk45CynMPtva0rtK/RB26
            // 7XC9wlRna4b3Ln8ew3q1ZcBjXwD4ppbTlmwAfQIaZTGJUgQbdsO9YA==
            // -----END RSA PRIVATE KEY-----
            // `,
            KeyPath: "/Users/username/.ssh/id_rsa",
            Port:    "22",
            Timeout: 60 * time.Second,
        }
    
        // Call Run method with command you want to run on remote server.
        stdout, stderr, done, err := ssh.Run("ls -al", 60*time.Second)
        // Handle errors
        if err != nil {
            panic("Can't run remote command: " + err.Error())
        } else {
            fmt.Println("don is :", done, "stdout is :", stdout, ";   stderr is :", stderr)
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用