V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
lysS
V2EX  ›  Go 编程语言

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

  •  
  •   lysS · 2022-05-06 09:41:02 +08:00 · 1484 次点击
    这是一个创建于 693 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    4 条回复    2022-05-06 15:46:26 +08:00
    XieQing0428
        1
    XieQing0428  
       2022-05-06 10:00:08 +08:00   ❤️ 1
    sudo -S 是从标准输入读密码的,可以试试 echo "xxx" | sudo -S ...
    lolizeppelin
        2
    lolizeppelin  
       2022-05-06 10:27:21 +08:00
    好像终端密码输入有些特别
    具体可以参考 pexpect 实现
    python 写的比较容易看
    lolizeppelin
        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 才行
    julyclyde
        4
    julyclyde  
       2022-05-06 15:46:26 +08:00
    直接读 tty
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5844 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 06:16 · PVG 14:16 · LAX 23:16 · JFK 02:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.