1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| 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
} |