[Tutor] Passing arguments to & running a python script on a remote machine from a python script on local machine .

Dave Angel d at davea.name
Wed Sep 19 21:24:22 CEST 2012

On 09/19/2012 02:47 PM, ashish makani wrote:
> Hi PyTutor Folks
> Here is my situation
> 1. I have two machines. Lets call them *local & remote.*
> Both run ubuntu & both have python installed
> 2. I have a python script, *local.py*, running on *local *which needs to
> pass arguments ( 3/4 string arguments, containing whitespaces like spaces,
> etc ) to a python script, *remote.py* running on *remote* (the remote
> machine).
> I have the following questions:
> 1. What's the best way to accomplish my task ?
> I have researched quite a bit & so far found really conflicting & complex
> workarounds.
> I googled & found people using several libraries to accomplish ssh to
> remote machine & execute a command on remote machine.
> paramiko <http://www.lag.net/paramiko/> ( now forked into the ssh
> moduke<https://github.com/bitprophet/ssh>),
> fabric <http://docs.fabfile.org/en/1.4.3/index.html>,
> pushy<http://packages.python.org/pushy/>,etc
> People who have used any of these libraries, which one would you recommend,
> as the most apt (simple & easy to use, lightweight, best performance, etc)
> for my situation ?
> 2. I would prefer a solution, which does NOT require the installation of
> extra libraries on the local & remote machines.
> If installing external librar
> 3. Has anybody been able to do this using os.system ?
> I tried this
>>>> import os
>>>> *os.system ("ssh remoteuser at remote python remote.py arg1 arg2 arg3")*
> *
> *
> This worked, but if the arguments i tried to pass, had spaces, i was not
> able to 'escape' the spaces.
> Any & all explanations/links/code
> snippets/thoughts/ideas/suggestions/feedback/comments/ of the Python tutor
> community would be greatly appreciated.
> Thanks a ton
> cheers
> ashish
> email :
> ashish.makani
> domain:gmail.com
> *“The only way to do great work is to love what you do. If you haven’t
> found it yet, keep looking. Don’t settle. As with all matters of the heart,
> you’ll know when you find it.” - Steve Jobs (1955 - 2011)*

Since you prefer not installing any optional libraries, I'll just talk
about your os.system() mechanism. Is that adequate for you, other than
the problem with spaces?

If so, then why not test it with the real ssh, to figure out where the
quotes need to be to handle the spaces. Then once you have it working,
use single quotes around the whole thing when calling os.exec().

Something like (all untested):

os.system ('ssh remoteuser at remote python remote.py arg1 "arg 2 has spaces" arg3')

Or, more likely, build the arguments in separate variables, and use

os.system( 'ssh remoteuser at remote python remote.py "%s" "%s" "%s"' %
(arg1, arg2, arg3) )

that will fail if the argn already has quotes in it. You can get much
fancier with encoding, which will add backslashes in front of some
characters, etc. But keep it simple if you can.

I ought to give the obligatory: use the multiprocess module instead of
os.exec, but if something works for you, I'm not going to argue.



More information about the Tutor mailing list