我已閱讀了一堆示例,但沒有一個適用於此特定任務。Python - subprocess.Popen - ssh -t user @ host'service --status-all'
Python代碼:
x = Popen(commands, stdout=PIPE, stderr=PIPE, shell=True)
print commands
stdout = x.stdout.read()
stderr = x.stderr.read()
print stdout, stderr
return stdout
輸出:
[[email protected]]$ python helpers.py
['ssh', '-t', '[email protected]', ' ', "'service --status-all'"]
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[[email protected]]hostname [command]
爲什麼會出現這個錯誤? 使用os.popen(...)它工作,它至少執行,但我無法通過SSH隧道檢索遠程命令的輸出。
輸出表明您沒有向命令傳遞足夠/正確的參數。 –
刪除空白項目,並刪除最後一個參數的單引號。 – Keith
順便說一句,你使用公鑰認證?或密碼認證? – Keith