Go 代码:
func A() {
c := exec.Command("bash")
var i = bytes.NewBuffer(nil)
var o = bytes.NewBuffer(nil)
c.Stdin = i
c.Stdout = o
c.Stderr = o
if err := c.Start(); err != nil {
panic(err.Error())
}
i.Write([]byte("sudo echo abc"))
i.Write([]byte("1")) // root password
if err := c.Wait(); err != nil {
panic(err.Error())
}
fmt.Println("标准 /错误输出:", o.String())
}
1
XieQing0428 2022-05-06 10:00:08 +08:00 1
sudo -S 是从标准输入读密码的,可以试试 echo "xxx" | sudo -S ...
|
2
lolizeppelin 2022-05-06 10:27:21 +08:00
好像终端密码输入有些特别
具体可以参考 pexpect 实现 python 写的比较容易看 |
3
lolizeppelin 2022-05-06 10:32:36 +08:00
https://stackoverflow.com/questions/55351259/how-does-ssh-receive-password-from-tty
For security reasons, many programs requires a password interactively from users. Quite many programs uses the following kind of check before reading a password from stdin: if (isatty(STDIN_FILENO) == 0) { exit(EXIT_FAILURE); } 也就是说你要欺骗 ssh 进程让它认为 stdin 是 tty 才行 |
4
julyclyde 2022-05-06 15:46:26 +08:00
直接读 tty
|