简体   繁体   English

SSH从我的本地机器到linux主机,sudo到root用户

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

Used subprocess to ssh to host. 使用子进程ssh到主机。

Here is the code snippet i used to ssh and run commands as my user. 这是我用来ssh和运行命令作为我的用户的代码片段。 When i try to use sudo command i get error related to tty -- not a terminal (something like that) 当我尝试使用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="")

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. 您可以通过指定-t选项强制执行伪tty分配。

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

From man ssh man ssh

  -t 

Force pseudo-tty allocation. 强制伪tty分配。 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. 多个-t选项强制tty分配,即使ssh没有本地tty。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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