V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
lysS

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

  •  
  •   lysS · May 6, 2022 · 2149 views
    This topic created in 1456 days ago, the information mentioned may be changed or developed.

    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 replies    2022-05-06 15:46:26 +08:00
    kbac001
        1
    kbac001  
       May 6, 2022   ❤️ 1
    sudo -S 是从标准输入读密码的,可以试试 echo "xxx" | sudo -S ...
    lolizeppelin
        2
    lolizeppelin  
       May 6, 2022
    好像终端密码输入有些特别
    具体可以参考 pexpect 实现
    python 写的比较容易看
    lolizeppelin
        3
    lolizeppelin  
       May 6, 2022
    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  
       May 6, 2022
    直接读 tty
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2417 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 01:41 · PVG 09:41 · LAX 18:41 · JFK 21:41
    ♥ Do have faith in what you're doing.