2017-03-19 25 views
1

二手子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") 

回答

0

可以強制通過指定-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

相關問題