简体   繁体   English

使用子进程ssh和执行命令

[英]using subprocess to ssh and execute commands

I need to ssh into the server and execute few commands and process the response using subprocess. 我需要进入服务器并执行一些命令,并使用子进程处理响应。 Here's my code 这是我的代码

command = 'ssh -t -t buildMachine.X.lan; sudo su - buildbot ; build-set sets/set123'
print "submitting command"
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
print "got response"
response,err = result.communicate()
print response

This is getting stuck. 这被卡住了。 I have seen other threads talking about passing a list instead of string to subprocess and removing shell=True.. I did that too but didn't work. 我看到其他线程在谈论将列表而不是字符串传递给子进程并删除shell = True。我也这样做,但是没有用。

Ultimately I need the result of last command ie build-set in order to extract some information out of it.. help? 最终,我需要最后一条命令的结果,即build-set,以便从中提取一些信息。

I figured the solution by using univerio's comment 我通过使用univerio的评论找到了解决方案

The command needs to be 该命令需要

command = 'ssh -t -t buildMachine.X.lan \'sudo su - buildbot  \'build-set sets/set123\'\''

Individual commands are like argument to previous command. 各个命令就像上一个命令的参数。 This works. 这可行。

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

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