我正在尝试通过subprocess
执行python脚本。首先,我尝试通过以下代码执行本地计算机上存在的python脚本:
str = 'abc'
sub_result = subprocess.Popen([sys.executable,"./script.py"] + str)
这正常工作。现在,我正在尝试通过subprocess
执行远程计算机上存在的脚本。首先,我找不到像通过本地计算机那样通过[C0]进行操作的任何示例。然后,我尝试在以下代码中使用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
命令中提及password
以进行连接?
更新:基于@musiphil的回答,我修改了代码,没有使用:
ssh
但我收到此错误:
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).
,否则应提供argv样式的列表:
shell=True
请参见str = 'abc'
sub_result = subprocess.call(
['ssh', 'username@hostname', 'python', './script.py', str])
。
您可以在https://docs.python.org/2/library/subprocess.html#subprocess.call命令的帮助下在ssh上提及密码。
sshpass