我有以下代码来检查到给定主机的SSH连接是否正常工作。
func IsSSHConnectionWorking(Host Host) bool {
cmd :=
fmt.Sprintf("ssh -o \"Passwordauthentication no\" -i %s -p %s %s@%s",Host.IdentityFile,Host.Port,Host.User,Host.IP)
fmt.Printf("Connecting to %s with %s",Host.IP,cmd)
output,err := exec.Command("bash","-c",cmd).Output()
if err != nil {
log.Println(err)
log.Printf("Failed to connect to Host: %s\n Error: %s",output)
return false
}
return true
}
我已经注意到,尽管参数错误,但返回到程序的错误是exit status 255
,输出为空。
在CLI本身上执行有问题的命令将返回user@host: Permission denied (publickey,password).
有没有办法捕捉到这个?