rudimentary ssh cmd wrapper with 1 small (and hopefully obvious) bug
Dreico-Hyper-Dynamics
dreico at wanadoo.fr
Sun Mar 2 17:33:55 EST 2003
Greetings python folks !
In a fit of curiosity to code some Python for the 1st time ever, as
a little project I made a rudimentary ssh command wrapper called
brussh, the idea of which is to send commands
to several machines at the same time (so far using a shell script loop
and working on "1 target at a a time"), & working ideally in situations
where ssh is configged to not need a passwd, using RSA/DSA keys
or whatever you like.
$ host001 > brush
USAGE:
You need to enter a hostname followed by some commands, like this:
brush <hostame> <commands> <commands> ...
It works fine except in situations where there is no output
from the ssh command that it's throwing at the target i.e.
$ host001 > brush host002 'ps -ef | grep ZZZZZZZZ'
===== HOST002 OUTPUT ===============================>
Traceback (innermost last):
File "/home/a/ad/scripts/brush", line 84, in ?
print "=====", node_in_caps, "OUTPUT
===============================>\n", the_command()
File "/home/a/ad/scripts/brush", line 65, in the_command
return command_results
NameError: command_results <<< ugly crash :(
Otherwise it does what you expect:
$ host001 > brush host002 'ps -ef | grep syslog'
===== HOST002 OUTPUT ===============================>
root 538 1 0 Feb25 ? 00:22:04 syslogd -m 0
root 14624 1 0 Feb28 ? 00:00:05 /usr/sbin/notd -l syslog --
/usr
$ host001 >
---
Where this gets lost (and I'm not sure I fully understand what's going on in
this bit of code) is here:
############################################################################
#
### Function "the_command"
def the_command():
payload = string.join(sys.argv[1:])
sshcmd = 'ssh -1 -o StrictHostKeyChecking=no -l root ' + payload
output = os.popen('{ ' + sshcmd + '; } 2>&1', 'r')
while 1:
line = output.readlines()
if not line: break <<<<<<< is this
the right way to handle a
else: <<<<<<<
possible 'null string output" situation ?
command_response = string.join(line[:])
return command_response
############################################################################
#
#
# MAIN PROGRAM
#
############################################################################
#
import string, os, sys
argnum = len(sys.argv)
if argnum < 3: Usage()
else:
machine = sys.argv[1]
node_in_caps = string.upper(machine)
listening_status = is_it_listening()
if listening_status == 1:
print "=====", node_in_caps, "OUTPUT
===============================>\n", the_command() <<<
else:
print "X ===", node_in_caps, "IS NOT CONTACTABLE ==================
X\n"
sys.exit(1)
I realise it's just a simple wrapper written by a beginner at this stage,
but
even so, can anyone give me any pointers as to what the obvious mistake
is ? (hopefully obvious to you in this case =)))
Thanks for you comments,
Andrei
More information about the Python-list
mailing list