除了标准/错误输出,还有其他输出吗?为什么捕获 root 密码的输入不到?

2022-05-06 09:41:02 +08:00
 lysS

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())
}

1509 次点击
所在节点    Go 编程语言
4 条回复
XieQing0428
2022-05-06 10:00:08 +08:00
sudo -S 是从标准输入读密码的,可以试试 echo "xxx" | sudo -S ...
lolizeppelin
2022-05-06 10:27:21 +08:00
好像终端密码输入有些特别
具体可以参考 pexpect 实现
python 写的比较容易看
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 才行
julyclyde
2022-05-06 15:46:26 +08:00
直接读 tty

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/851094

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX