【发布时间】:2014-10-08 18:25:21
【问题描述】:
我需要 ssh 进入服务器并执行一些命令并使用子进程处理响应。这是我的代码
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
这卡住了。我看到其他线程谈论将列表而不是字符串传递给子进程并删除 shell=True.. 我也这样做了,但没有奏效。
最终我需要最后一个命令的结果,即 build-set 以便从中提取一些信息..帮助?
【问题讨论】:
-
您需要将要远程执行的命令作为最后一个参数传递给
ssh,而不是作为以;分隔的附加命令。 -
你有无密码登录服务器吗?
-
@AshuPachauri 是的 - 私钥已经存储在我的机器中,所以不需要再次提供密码。
-
@user2812714 您必须执行
ssh host 'foo bar'才能远程运行foo bar。如果您执行ssh host; foo bar,您正在本地运行ssh host,等待ssh退出,然后执行foo bar本地。 -
你真的想把它分成一个列表而不是使用
shell=True。很难弄清楚如何将ssh参数与远程主机参数分开,而无需处理额外的引用层。
标签: python ssh subprocess