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
'StrictHostKeyC hecking no' %s '%s' |awk '{print \"%s:\"$0}' "
'''%(host,comma nd,host), timeout=30)
child.setlog(sy s.stdout)
child.expect(pe xpect.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 'StrictHostKeyC hecking 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 'StrictHostKeyC hecking 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 3 3479
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 'StrictHostKeyC hecking no' %s '%s' |awk '{print \"%s:\"$0}' " '''%(host,comma nd,host), timeout=30)
child.setlog(sy s.stdout) child.expect(pe xpect.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 'StrictHostKeyC hecking 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 'StrictHostKeyC hecking 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(c omm, 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.
I can't seem to get that to work either.
child =
pexpect.spawn('/bin/sh',args=['-c','/usr/bin/ssh','-t','-o','StrictHostK eyChecking
no',host,comman d,'|','awk','{p rint %s:$0}'%host], timeout=30)
Complains its getting the wrong arguments to ssh.
Eli
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',
'StrictHostKeyC hecking no',host,comman d])
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)
iD8DBQFDWDd6Jd0 1MZaTXX0RAhb9AK CkRn/iBtG2KM2D7OCpIZ QE8A7YPACgpOCA
swOL+oDJBOd4NjI 5cC5Pk+o=
=VkjM
-----END PGP SIGNATURE----- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 also contain escaped
octal or hex such as \001 or \x09.
The escapes are coming into sys.argv without shell interpretation. Do I
need to use the compile module to make this work? Any suggestions?
===
|
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
|
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 newsgroups I learned that there
is a function in stdlib.h called system.
int system(const char *command);
First question, is the system command ANSI compliant. Because I include
<cstdlib> and write std::system(command.c_str()); it looks like an...
|
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.
JavaScripts escape() function converts all non-alphanumerical chars to %xx,
and all unicode chars to %uxxxx (where xxxx is unicode charcode). I'd like
to have C# function that get this escaped string back to normal at the
server side. Perhaps there...
|
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 open connection
string sqlStatement="SELECT oid FROM formatos WHERE formato='cd\\'s'";
| |
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 strings themselves to.
strSQL = "INSERT INTO tblKlanten (Naam, Voornaam, Adres, Postnummer,
Telefoon, Fax, Gsm, Email, Gastvrouw, Matras, Lattenbodum, Waveflex,
Donsdeken, Btwnummer, Geboortedatum, Echtgenoot, Opmerkingen, Aankooplb)"
strSQL += "...
|
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. Any idea if this is a bug?
if (e.KeyChar == '\e')
{
this.Close();
}
|
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 application running on the background
and return to the cmd prompt, then I can come back to my application by
typing "exit"
any ideas?
|
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 within the current console.
Jon
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |