使用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
donglin9068
donglin9068 为什么不检查为什么SSH_AUTH_SOCK为空?
接近 3 年之前 回复

1个回答

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

}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问