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)
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败