【发布时间】:2015-05-03 13:31:42
【问题描述】:
我正在尝试通过subprocess 执行 python 脚本。首先,我尝试通过以下代码执行本地机器上的 python 脚本:
str = 'abc'
sub_result = subprocess.Popen([sys.executable,"./script.py"] + str)
这工作正常。现在我正在尝试通过subprocess 执行远程机器上的脚本。首先,我找不到任何关于如何通过subprocess.Popen() 进行操作的示例,就像我在本地机器上所做的那样。然后我尝试在以下代码中使用subprocess.call(),但我遇到了问题:
str = 'abc'
sub_result = subprocess.call('ssh username@hostname python ./script.py' + str)
我收到以下错误:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我做错了什么以及如何在ssh 命令中提及password 进行连接?
更新:根据@musiphil 的回答,我修改了我的代码,但我没有使用:
str = 'abc'
sub_result = subprocess.call(['ssh', 'username@hostname', 'python', './script.py'] + str)
但我收到此错误:
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
【问题讨论】:
-
在遇到 SSH 问题之前,您正在运行的代码应该引发 TypeError(只能将列表(而不是“str”)连接到列表)。
-
@jd。我将
str保留为列表。我忘了更新上面代码中的str -
@jd。我已经修改了上面更新部分的代码
-
['a']+'b'在 Python 中引发 TypeError。 -
每个问题保留一个问题。有两个问题:不正确的
subprocess用法以及如何通过ssh进行身份验证。 @musiphil 回答了前者。为后者提出一个单独的问题。
标签: python ssh subprocess