使用pubkey的golang ssh登录失败

时间:2017-10-12 02:11:27

标签: go ssh ssh-agent

在mac中,我可以使用密钥在没有密码的情况下登录rain01,在rain01中,我可以运行命令ssh tree01,然后我登录。但是使用golang ssh包,我被告知<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="clonedInput"> <input type="text" class="calculate" placeholder="Cabinet Width"> <div class="sum"></div> <div class="cal1">duplicate sum here</div> <div class="cal2">duplicate sum here</div> <div class="cal3">duplicate sum here</div> <div class="cal4">duplicate sum here</div> <div class="actions"> <button class="clone">Add</button> <button class="remove">Remove</button> </div> </div>为空,这是错误消息SSH_AUTH_SOCK 这是我的代码

dial unix: missing address

现在我有var ssh_auth_sock,但还有另一个问题

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
}

和主ssh配置是

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

1 个答案:

答案 0 :(得分:0)

尝试使用https://github.com/appleboy/easyssh-proxy软件包

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

}