我想自动为 bash 终端中的命令提供输入。
是否可以在 ssh 提示符之后以编程方式输入“是”?这是一个代表这个想法的脚本。
package main
import (
"bytes"
"io"
"os"
"fmt"
"os/exec"
)
func main() {
c1 := exec.Command("ssh","root@172.30.0.77")
r, w := io.Pipe()
c1.Stdout = w
c1.Stdin = r
c1.Start()
var b bytes.Buffer
b.Write([]byte("yes"))
fmt.Fprintf(&b, "\n")
b.WriteTo(os.Stdin)
c1.Wait()
w.Close()
}
# go run login.go
yes
The authenticity of host '172.30.0.77 (172.30.0.77)' can't be established.
RSA key fingerprint is 13:46:96:ff:ab:12:76:0e:24:6e:3e:7a:ee:c0:81:af.
Are you sure you want to continue connecting (yes/no)?