gpt4 book ai didi

使用pubkey的golang ssh登录失败

转载 作者:IT王子 更新时间:2023-10-29 01:47:16 26 4
gpt4 key购买 nike

在 mac 中,我可以使用 key 无密码登录 rain01,在 rain01 中,我可以运行命令 ssh tree01,然后我登录。但是使用 golang ssh 包,我被告知 SSH_AUTH_SOCK 是空的,并且这是错误消息 dial unix: missing address这是我的代码

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
}

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

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

主要的ssh配置是

StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
HostbasedAuthentication no
IgnoreRhosts yes
RhostsRSAAuthentication no

最佳答案

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

}

关于使用pubkey的golang ssh登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46700322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com