SSH到Linux主机和须藤以root用户

问题描述:

二手子ssh到主机。SSH到Linux主机和须藤以root用户

这里是代码片段我曾经SSH和运行命令作为我的用户。当我尝试使用sudo命令我得到有关TTY的错误 - 不是终端(类似的东西)

sshProcess = subprocess.Popen(['ssh', hostname], stdin=subprocess.PIPE, stdout = subprocess.PIPE, universal_newlines=True, bufsize=0) 
sshProcess.stdin.write("hostname\n") 
sshProcess.stdin.write("ls -ldr %s \n"%path) 
sshProcess.stdin.write("pwd \n") 
sshProcess.stdin.close() 
for line in sshProcess.stdout: 
if line == "END\n": 
break 
print(line,end="") 

,但我不能够低于命令运行

sshProcess.stdin.write("sudo su - serviceuser \n") 

可以强制通过指定-t选项来进行伪tty分配。

subprocess.Popen(['ssh -t', hostname], ..... 

man 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, e.g. when implementing menu services. Multiple -t optionsforce tty allocation, even if ssh has no local tty.

+0

我尝试了以前,但没有奏效 我得到以下错误 回溯(最近最后一次通话): 文件 “ssh.py”,行11, sshProcess = subprocess.Popen(['ssh -t',hostname],stdin = subprocess.PIPE,stdout = subprocess.PIPE,universal_newlines = True,bufsize = 0) 文件“/ usr/lib/python2 .7/subprocess.py“,l INE 711,在__init__ errread,ERRWRITE) 文件 “/usr/lib/python2.7/subprocess.py”,线1343,在_execute_child 加注child_exception OSERROR:[错误2]没有这样的文件或目录 – Springhills

+0

我加shell = True在Popen中,但我得到了其他错误 sshProcess = subprocess.Popen(['ssh -t',hostname],shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,universal_newlines = True,bufsize = 0) – Springhills

+0

usage:ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:] port] [-E log_file] [-e escape_char] [-F configfile] [-I PKCS11] [-i identity_file] [-J [用户@]主机[:端口]] [-L地址] [-l LOGIN_NAME] [-m mac_spec] [-O ctl_cmd] [-o选项] [-p端口] [-Q query_option] [-R地址] [-S ctl_path] [-W主机:端口] [-w local_tun [:remote_tun]] [ user @] hostname [command] – Springhills