LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2009, 04:41 PM   #1
honestiron
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Rep: Reputation: 0
ssh remote command preserve quoted argument


Hi all,

I have a python script on one server (serv_one) and I am trying to execute it remotely from another (serv_two). The python script takes an argument with spaces. If I execute it locally:

Code:
foo@serv_one> script.py --o "arg one"
"arg one" is preserved, of course. ( argv = [ '--o', 'arg one' ] )

However, when I execute it remotely:

Code:
foo@serv_two> ssh ... foo@serv_one script.py --o "arg one"
the double quotes around "arg one" are dismissed ( argv = [ '--o', 'arg', 'one' ].

I've tried many combinations of single quotes/double quotes/backslashes, etc, to no avail.

One hack solution I came up with, since I have the flexibility, was to replace all spaces in the quoted argument with a character that would be invalid in the argument (before the ssh call), and replace those with spaces in script.py. I would probably like to avoid this solution if at all possible.

I've actually had some time looking for a solution to this on google and surprisingly not much help -- considering this seems like it would be fairly common.

Anyway, if anyone knows how to solve this that would be great.

Both machines are running 64-bit RH5.3, python 2.5.

Thanks!
 
Old 09-17-2009, 05:28 PM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 356

Rep: Reputation: 43
Have you tried
Code:
foo@serv_two> ssh ... foo@serv_one "script.py --o arg one"
 
Old 09-17-2009, 05:41 PM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by honestiron View Post
Hi all,

I have a python script on one server (serv_one) and I am trying to execute it remotely from another (serv_two). The python script takes an argument with spaces. If I execute it locally:

Code:
foo@serv_one> script.py --o "arg one"
"arg one" is preserved, of course. ( argv = [ '--o', 'arg one' ] )

However, when I execute it remotely:

Code:
foo@serv_two> ssh ... foo@serv_one script.py --o "arg one"
the double quotes around "arg one" are dismissed ( argv = [ '--o', 'arg', 'one' ].

I've tried many combinations of single quotes/double quotes/backslashes, etc, to no avail.

One hack solution I came up with, since I have the flexibility, was to replace all spaces in the quoted argument with a character that would be invalid in the argument (before the ssh call), and replace those with spaces in script.py. I would probably like to avoid this solution if at all possible.

I've actually had some time looking for a solution to this on google and surprisingly not much help -- considering this seems like it would be fairly common.

Anyway, if anyone knows how to solve this that would be great.

Both machines are running 64-bit RH5.3, python 2.5.

Thanks!
Try this:

Code:
foo@serv_two> ssh ... foo@serv_one "script.py --o \"arg one\""
 
Old 09-17-2009, 06:04 PM   #4
honestiron
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TBC Cosmo View Post
Have you tried
Code:
foo@serv_two> ssh ... foo@serv_one "script.py --o arg one"
Thanks Cosmo, but that one didn't work either.

Quote:
Originally Posted by lutusp View Post
Try this:

Code:
foo@serv_two> ssh ... foo@serv_one "script.py --o \"arg one\""
Lutusp, also negative.

Also just tried

Code:
foo@serv_two> ssh ... foo@serv_one script.py --o '"arg one"'
Scratching my head here...

Last edited by honestiron; 09-17-2009 at 06:07 PM.
 
Old 09-17-2009, 11:50 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Doesn't Python have a getopts equiv ?
http://docs.python.org/library/getopt.html
NB: I'm not a Python programmer...
 
Old 09-18-2009, 12:08 AM   #6
honestiron
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Doesn't Python have a getopts equiv ?
http://docs.python.org/library/getopt.html
NB: I'm not a Python programmer...
Yea I am using that to parse the command line options but before getopt is even called, "arg one" has been split into two separate arguments.
 
Old 09-18-2009, 09:23 AM   #7
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
this works for me

foo@serv_two> ssh ... foo@serv_one script.py --o \"arg one\"

ssh foo@serv_one /tmp/spit.pl --o \"arg one\"
foo@serv_one's password:
[--o]
[arg one]

and that script is
Code:
#!/usr/bin/perl

foreach $arg (@ARGV) {
        print "[$arg]\n";
}
 
Old 09-18-2009, 12:23 PM   #8
honestiron
LQ Newbie
 
Registered: Sep 2009
Posts: 4

Original Poster
Rep: Reputation: 0
So, I had left out a couple of details for simplicity's sake. I'm actually calling the whole command through python's popen3 module, which should've worked fine but I was unaware of certain scripts on the remote client side that process the command (to make sure it's valid, etc) and it was there that quoted arguments were getting mangled!

Anyway, problem identified. Thanks for everyone's help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: how do I pass a double quoted string to a command? Nylex Programming 8 04-18-2007 09:36 AM
logging, remote ssh, scripts, and the at command gctaylor1 Programming 4 01-29-2007 08:22 AM
SSH remote command not timing out tajsss Other *NIX 5 04-28-2005 06:57 AM
Adding users via ssh remote command enolc Linux - Software 2 05-27-2004 06:48 AM
remote command over ssh, password prompt linowes Linux - General 2 10-27-2002 08:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration