简体   繁体   中英

Better way of running a python script remotely

I have a python script on a remote machine which I want to execute from my local machine. It takes in a few arguments and this is how I would run it if I were to run it on that machine.

python python_parallel.py --num=10 --ssh=/home/user1/path/file.txt

Currently I have a python code in my local machine which runs the above script:

from optparse import OptionParser
parser.add_option("-n", "--num", type="int", dest="num_spice",help="Enter the number")
parser.add_option("-s", "--ssh", dest="ssh_txt",help="Enter the path to the text file")
num_spice=options.num_spice
ssh_txt=options.ssh_txt

(options, args) = parser.parse_args()

os.system('ssh user1@10.100.10.201 python /home/user1/path/python_parallel.py --num=%s --ssh=%s' %(num_spice, ssh_txt) )

Is there a better way of doing this? I tried the solution at this link , but it gave me an error "ImportError: No module named ssh"

I recommend you look at the plumbum module for doing things like this.

Its a pretty cool and easy way to run local commands and you can do the same with remote commands quite easily (with a context manager).

Have you considered using Fabric ? It's really easy to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM