我正在尝试进入一组不支持常规shell的服务器。 / crypto / ssh库不适用于这些,因此我正在通过exec.Command
使用直接ssh。
现在的问题是,如果我通过-o StrictHostKeyChecking=no
,该命令就会挂起,即:
exec.Command("ssh", "-o", "StrictHostKeyChecking=no", config.User + "@" + host)
但适用于简单的
exec.Command("ssh", "-T", config.User + "@" + host)
完整的代码块在这里(基本上只需要像这样将命令通过管道传递到ssh连接
echo hostname | ssh -T -o StrictHostKeyChecking=no user1@db1-ar6.name.local
:
p1 := exec.Command("echo", config.Command)
p2 := exec.Command("ssh", "-o", "StrictHostKeyChecking=no", config.User + "@" + host)
//p2 := exec.Command("ssh", "-T", config.User + "@" + host) //doesn't work
fmt.Println(p2) //checking output
var stdout, stderr bytes.Buffer
p2.Stdout = &stdout
p2.Stderr = &stderr
p2.Stdin, _ = p1.StdoutPipe() // p1 pipe to p2 ssh
err1 := p2.Start() // start ssh command but dont wait for it to finish
if err1 != nil {
return errors.Wrap(err1, "starting ssh session failed")
}
err2 := p1.Run() // run pipe command and wait for it to finish
if err2 != nil {
return errors.Wrap(err2, config.Command+" failed", )
}
err3 := p2.Wait() // ssh wait for pipe to close
if err3 != nil {
return errors.Wrap(err3, "ssh wait failed")
}
输出为,并且等待功能失败:
&{/usr/bin/ssh [ssh -o StrictHostKeyChecking=no user1@db1-ar6.name.local] [] <nil> <nil> <nil> [] <nil> <nil> <nil> <nil> <nil> false [] [] [] [] <nil> <nil>}
2019/05/24 10:28:41 ssh wait failed: exit status 255