创build一个伪terminal使sudo开心

我需要自动configuration一个实例(正在运行Fedora 17),以下是最初的事实:

  • 我有基于ssh-key的访问远程用户( cloud
  • 该用户通过sudo具有无密码root权限。

手动configuration就像login并运行sudo su -一样简单,并且拥有它,但是我想完全自动化这个过程。 诀窍是系统默认启用了sudorequiretty选项,这意味着尝试做这样的事情:

 ssh remotehost sudo yum -y install puppet 

会失败:

 sudo: sorry, you must have a tty to run sudo 

现在我正在解决这个问题,首先推倒一个在伪terminal上运行命令的小Python脚本:

 import os import sys import errno import subprocess pid, master_fd = os.forkpty() if pid == 0: # child process: now that we're attached to a # pty, run the given command. os.execvp(sys.argv[1], sys.argv[1:]) else: while True: try: data = os.read(master_fd, 1024) except OSError, detail: if detail.errno == errno.EIO: break if not data: break sys.stdout.write(data) os.wait() 

假设这个名字是pty ,那么我可以运行:

 ssh remotehost ./pty sudo yum -y install puppet 

这工作正常,但我想知道是否有解决scheme已经可用,我没有考虑。

  • 我通常会考虑expect ,但是这个系统默认没有安装。
  • screen可以做到这一点捏,但最好的我想出了:

     screen -dmS sudo somecommand 

    …哪些工作,但吃的输出。

有没有其他的工具可以为我分配一个伪terminal,这些terminal一般都可用?

你希望-t选项ssh :

  -t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, eg when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty. 

如果脚本将以非交互方式运行,则可能需要使用-tt