简体   繁体   中英

SSH from my local machine to linux host and sudo to root user

Used subprocess to ssh to host.

Here is the code snippet i used to ssh and run commands as my user. When i try to use sudo command i get error related to tty -- not a terminal (something like that)

 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="")

But i am not able to run below command

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

You can force a pseudo-tty allocation by specifying -t option.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM