[英]Running script from remote server using ssh in python
我想從本地計算機運行python腳本。 但是名為script.py
的python腳本位於遠程服務器中,它具有一些自變量和參數。 我努力了:
#!/usr/bin/python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='x.x.x.x', port=22, username='root', password='passwd')
stdin, stdout, stderr=ssh.exec_command('python /root/file/script.py') #It has some argument I want to use them from my local machine
for i in stdout.readlines():
print i.strip('\r\n')
ssh.close()
script.py
有一些參數。 我應該如何更改此腳本才能使用本地計算機上的script.py
參數?
由於ssh不支持參數列表,因此它比人們想象的要棘手得多:
import sys
try:
from pipes import quote # python 2
except ImportError:
from shlex import quote # python 3
ssh.exec_command('python /root/file/script.py ' +
' '.join([quote(i) for i in sys.argv[1:]])
)
當運行python myprogram.py argument\\ with" quotes\\" and spaces"
,這應該將'argument with quotes" and spaces'
參數作為1參數傳遞給另一個程序。 盡管我不保證paramiko總是做正確的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.