473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

nested escape chars in a shell command

I'm try run an ssh command in pexpect and I'm having trouble getting
everything escaped to do what i want.

Here's a striped down script showing what i want to do.

--
#!/usr/bin/env python
import pexpect
import sys
if len(sys.argv) < 3:
print "ssh.py host command"
sys.exit(1)

host = sys.argv[1]
command = sys.argv[2]

child = pexpect.spawn('''sh -x -c "stty -echo ; ssh -t -o
'StrictHostKeyChecking no' %s '%s' |awk '{print \"%s:\"$0}' "
'''%(host,command,host), timeout=30)

child.setlog(sys.stdout)
child.expect(pexpect.EOF)
--

The problem in the pexpect.spawn line, It doesn't like the \"%s:\" part
of the awk command. This is necessary so i can see what server the
command is running on, In the full script the command will be running
on about 100 servers at a time.
It parses out into:
+ stty -echo
+ ssh -t -o 'StrictHostKeyChecking no' testserver date
+ awk '{print testserver:$0}'
It totally strips out the "

The stty -echo is required because part of what the program does is it
tries to remember any passwords that are asked for, So you can run a
command like "su -c id" and it will remember roots password for the
next
server and try that. -echo keeps the root password from being echoed to
the screen.

The second problem with the command is while "su -c id" works (taking
out the awk part) running any command with more then one word after the
-c in su fails, It strips out the '
like so:
../sshexpect testserver "su -c 'ls -l /root'"
+ stty -echo
+ ssh -t -o 'StrictHostKeyChecking no' testserver 'su -c ls' -l /root
su: user /root does not exist

I have tried every combination of escaping i can think of can i can't
get either problem solved.

Any ideas?

Eli

Oct 18 '05 #1
3 3470
Eli Criffield wrote:
I'm try run an ssh command in pexpect and I'm having trouble getting
everything escaped to do what i want.

Here's a striped down script showing what i want to do.

--
#!/usr/bin/env python
import pexpect
import sys
if len(sys.argv) < 3:
print "ssh.py host command"
sys.exit(1)

host = sys.argv[1]
command = sys.argv[2]

child = pexpect.spawn('''sh -x -c "stty -echo ; ssh -t -o
'StrictHostKeyChecking no' %s '%s' |awk '{print \"%s:\"$0}' "
'''%(host,command,host), timeout=30)

child.setlog(sys.stdout)
child.expect(pexpect.EOF)
--

The problem in the pexpect.spawn line, It doesn't like the \"%s:\" part
of the awk command. This is necessary so i can see what server the
command is running on, In the full script the command will be running
on about 100 servers at a time.
It parses out into:
+ stty -echo
+ ssh -t -o 'StrictHostKeyChecking no' testserver date
+ awk '{print testserver:$0}'
It totally strips out the "

The stty -echo is required because part of what the program does is it
tries to remember any passwords that are asked for, So you can run a
command like "su -c id" and it will remember roots password for the
next
server and try that. -echo keeps the root password from being echoed to
the screen.

The second problem with the command is while "su -c id" works (taking
out the awk part) running any command with more then one word after the
-c in su fails, It strips out the '
like so:
./sshexpect testserver "su -c 'ls -l /root'"
+ stty -echo
+ ssh -t -o 'StrictHostKeyChecking no' testserver 'su -c ls' -l /root
su: user /root does not exist

I have tried every combination of escaping i can think of can i can't
get either problem solved.

Any ideas?

Eli


You can pass the argument list of your command to
pexpect.spawn(comm, args=['arg1','arg2'])
If the argument list is empty, pexpect tries to get the arguments
from the comm you passed to it. I guess this gives you problems.

Try using the args parameter.
Simplest would be args=[' '] just to avoid the processing.
Oct 19 '05 #2
I can't seem to get that to work either.

child =
pexpect.spawn('/bin/sh',args=['-c','/usr/bin/ssh','-t','-o','StrictHostKeyChecking
no',host,command,'|','awk','{print %s:$0}'%host], timeout=30)

Complains its getting the wrong arguments to ssh.

Eli

Oct 20 '05 #3
I think you're mistaken about how 'sh -c' works. The next argument after "-c" is
the script, and following arguments are the positional arguments. (what, you've
never used -c in conjunction with positional arguments? me either!)

Example:
------------------------------------------------------------------------
$ /bin/sh -c 'echo $#' a b c

# i.e., a blank line
$ /bin/sh -c 'echo $# 0=$0 1=$1 2=$2' a b c
2 0=a 1=b 2=c
# i.e., a, b, c went to positional arguments
------------------------------------------------------------------------

Additionally, unless pexpect.spawn behaves differently than os.spawnv,
"-c" is actually going to /bin/sh's argv[0], and you end up trying to execute
/usr/bin/ssh as a shell script, which is probably not what you want!

------------------------------------------------------------------------
def shellquote(arg):
# shell quoting technique I first read about on the 'git' development list
# Everything is safely quoted inside a ''-quoted string, except a ' itself,
# which can be written as '\'' (a backslash-escaped ' outside of the ''-quoted
# string)
return "'" + arg.replace("'", "'\\''") + "'"

def shellquotelist(args):
return " ".join([shellquote(arg) for arg in args])

# XXX: you may wish to assemle 'command' using shellquotelist too
command1 = shellquotelist(['/bin/sh', '-c','/usr/bin/ssh','-t','-o',
'StrictHostKeyChecking no',host,command])
command2 = shellquotelist(['awk','{print %s:$0}'%host])

child = \
pexpect.spawn('/bin/sh',
args=['/bin/sh', '-c', command1 + "|" + command2],
timeout=30)
------------------------------------------------------------------------

Finally, in your case the use of awk would seem to be gratuitous. Instead,
prepend the hostname to each line when you read the lines in. (this could easy
or hard depending on the structure of the code where you read the output
generated by child---if you do it with readline() or by iterating over the file,
it is probably easy)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDWDd6Jd01MZaTXX0RAhb9AKCkRn/iBtG2KM2D7OCpIZQE8A7YPACgpOCA
swOL+oDJBOd4NjI5cC5Pk+o=
=VkjM
-----END PGP SIGNATURE-----

Oct 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul Watson | last post by:
How can I get the escapes from a command line parameter interpreted? The user provides a string on the command line. The string might contain traditional escapes such as \t, \n, etc. It might...
8
by: Joe | last post by:
I'm using Python 2.4 on Windows XP SP2. I'm trying to receive a command line argument that is a newline (\n) Here is the command line to use sample.py "\n" Here is a sample.py script
8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
2
by: Pavils Jurjans | last post by:
Hello, I am looking fow C# equivalent of JavaScripts escape() and unescape() functions. I need to use C# function at the server side and then be able to use the opposite at the client side. ...
7
by: Luminal | last post by:
Greetings I'm having some problems on my C# application. I'm using an access database and I'm not able to do select queries with the ' character. My code is this: // some previous code like...
6
by: Stijn Vanpoucke | last post by:
Hi, I've made a program with an access database. In my sql insert command I need to use escape characters to insert text strings but te problem is that I want to use escape chars in my text...
15
by: pkaeowic | last post by:
I am having a problem with the "escape" character \e. This code is in my Windows form KeyPress event. The compiler gives me "unrecognized escape sequence" even though this is documented in MSDN....
4
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi, I am writing a command line application and I would like to implement a functionality similar to the "!" command in ftp.exe (that comes with most windows distros) so that I can leave my...
0
by: Jon Skeet [C# MVP] | last post by:
On Jun 17, 12:02 pm, "Jon Skeet " <sk...@pobox.comwrote: Sorry, ignore this answer - it's completely brain dead; I'd misunderstood the question. I don't know a way of launching an command shell...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.