Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way of running a python script remotely

Tags:

python

ssh

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 [email protected] 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"

like image 838
Nanditha Avatar asked Feb 18 '23 06:02

Nanditha


1 Answers

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).

like image 178
itai Avatar answered Feb 19 '23 21:02

itai