簡體   English   中英

通過子進程在遠程機器上執行 python 腳本時出錯

[英]Error in executing a python script at remote machine through subprocess

我正在嘗試通過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).

您應該給出一個 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

您可以在sshpass命令的幫助下在 ssh 上提及密碼。

    ['sshpass', '-p', 'password', 'ssh','username@%s' % address]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM