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

2012-09-20 Thread Modulok
> I forgot you need to escape special characters in the arguments. You
> can add quoting and escape special characters at the same time with
> the undocumented function pipes.quote:
>
> import pipes
>
> args = tuple(pipes.quote(arg) for arg in (arg1, arg2, arg3))
> cmd = 'python test.py %s %s %s' % args
>
> Notice there are no longer quotes around each %s in cmd. Python 3.3
> will have shlex.quote:
>
> http://docs.python.org/dev/library/shlex.html#shlex.quote

Why is ``pipes.quote`` undocumented? It's useful.

-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-20 Thread eryksun
On Thu, Sep 20, 2012 at 6:25 AM, eryksun  wrote:
>
> I forgot you need to escape special characters in the arguments. You
> can add quoting and escape special characters at the same time with
> the undocumented function pipes.quote:
>
> import pipes
>
> args = tuple(pipes.quote(arg) for arg in (arg1, arg2, arg3))
> cmd = 'python test.py %s %s %s' % args

FYI, here's what pipes.quote does. If the string has any special
characters, it first replaces any single quotes in the string with
'"'"' and then wraps the string in single quotes. For example
"abc'def" becomes "'abc'\"'\"'def'". The shell doesn't use special
characters ($`\) in single-quoted strings.

Source:

def quote(file):
"""Return a shell-escaped version of the file string."""
for c in file:
if c not in _safechars:
break
else:
if not file:
return "''"
return file
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + file.replace("'", "'\"'\"'") + "'"
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-20 Thread ashish makani
On Thu, Sep 20, 2012 at 3:43 AM, eryksun  wrote:

> On Wed, Sep 19, 2012 at 2:47 PM, ashish makani 
> wrote:
> >
> > I tried this
>  import os
>  os.system ("ssh remoteuser@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.
>
> Presuming "remote" has an SSH server running, and that a firewall
> isn't blocking port 22, and that you've enabled logging in without a
> password (i.e. ssh-keygen), try the following:
>
> import sys
>> import subprocess
>> user = remoteuser
>> hostname = remote
>> cmd = 'python remote.py "%s" "%s" "%s"' % (arg1, arg2, arg3)
>> try:
>> out = subprocess.check_output(['ssh', '%s@%s' % (user,
>> hostname), cmd])
>> except subprocess.CalledProcessError as e:
>> print >>sys.stderr, str(e)   # handle/log the error, retry
>
>
This worked like a charm.
Thank you so much.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-20 Thread eryksun
On Wed, Sep 19, 2012 at 6:13 PM, eryksun  wrote:
>
> cmd = 'python remote.py "%s" "%s" "%s"' % (arg1, arg2, arg3)
>
> try:
> out = subprocess.check_output(['ssh', '%s@%s' % (user, hostname), 
> cmd])

I forgot you need to escape special characters in the arguments. You
can add quoting and escape special characters at the same time with
the undocumented function pipes.quote:

import pipes

args = tuple(pipes.quote(arg) for arg in (arg1, arg2, arg3))
cmd = 'python test.py %s %s %s' % args

Notice there are no longer quotes around each %s in cmd. Python 3.3
will have shlex.quote:

http://docs.python.org/dev/library/shlex.html#shlex.quote


Also, if you don't care about the output, use subprocess.check_call()
instead. However, the connection still waits for the remote shell's
stdout to close. If remote.py is long-running, redirect its stdout to
a log file or /dev/null and start the process in the background (&).
For example:

cmd = 'python remote.py %s %s %s >/dev/null &' % args

With this command remote.py is put in the background, stdout closes,
the forked sshd daemon on the server exits, and ssh on the client
immediately returns.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-19 Thread eryksun
On Wed, Sep 19, 2012 at 2:47 PM, ashish makani  wrote:
>
> I tried this
 import os
 os.system ("ssh remoteuser@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.

Presuming "remote" has an SSH server running, and that a firewall
isn't blocking port 22, and that you've enabled logging in without a
password (i.e. ssh-keygen), try the following:

import sys
import subprocess

user = remoteuser
hostname = remote
cmd = 'python remote.py "%s" "%s" "%s"' % (arg1, arg2, arg3)

try:
out = subprocess.check_output(['ssh', '%s@%s' % (user, hostname), cmd])
except subprocess.CalledProcessError as e:
print >>sys.stderr, str(e)   # handle/log the error, retry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-19 Thread Dave Angel
On 09/19/2012 03:45 PM, ashish makani wrote:
> Thanks a ton for the prompt reply & the great suggestions, Dave.
> 

Please don't top post.  In this mailing list, the convention is to put
your remarks after the part you're quoting (which usually isn't the
entire message).  That way, if the context gets complex, it's easier to
see who wrote what, and in what order.

> 1.   A colleague gave this exact same suggestion
> os.system ('ssh remoteuser@remote python remote.py arg1 "arg 2 has spaces"
> arg3')
> 
> I was thinking spaces is my problem, so i initially tested the following
> (no ssh)
> os.system('python remote.py arg1 "arg 2 has spaces" arg3')  & it works :)
> 
> But sadly,  os.system ('ssh remoteuser@remote python remote.py arg1 "arg 2
> has spaces" arg3')   does not :(

Did you try it the way I suggested above?  Make it work in ssh, then
worry about how python can build that command string.

> 
> 2. Also, you mentioned os.exec
> Which would you recommend ?
> os.system or os.exec ?

Since there's no  os.exec, it was just an brain-hiccup on my part.



So, back to the problem.  Once you've got it working in ssh, then we can
figure how to make os.exec, or other python execution mechanism, work.

-- 

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-19 Thread ashish makani
Thanks a ton for the prompt reply & the great suggestions, Dave.

1.   A colleague gave this exact same suggestion
os.system ('ssh remoteuser@remote python remote.py arg1 "arg 2 has spaces"
arg3')

I was thinking spaces is my problem, so i initially tested the following
(no ssh)
os.system('python remote.py arg1 "arg 2 has spaces" arg3')  & it works :)

But sadly,  os.system ('ssh remoteuser@remote python remote.py arg1 "arg 2
has spaces" arg3')   does not :(

2. Also, you mentioned os.exec
Which would you recommend ?
os.system or os.exec ?

Thanks a lot,

cheers
ashish



On Thu, Sep 20, 2012 at 12:54 AM, Dave Angel  wrote:

> 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  ( now forked into the ssh
> > moduke),
> > fabric ,
> > 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@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@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@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.
>
>
>
>
> --
>
> DaveA
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-19 Thread Dave Angel
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  ( now forked into the ssh
> moduke),
> fabric ,
> 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@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@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@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.




-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-09-19 Thread ashish makani
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  ( now forked into the ssh
moduke),
fabric ,
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@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)*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor