rudimentary ssh cmd wrapper with 1 small (and hopefully obvious) bug

Andrei Doicin Andrei.Doicin at cern.ch
Mon Mar 3 09:10:27 EST 2003


Thanks for the tips John.

It's strange, but before I delved into your suggestions, I cleaned up the
indents, after happening
to read yesterday that it's not a good idea to use a combination of spaces
and tabs, which was the case
in the script before. So anyway I changed all indents to multiples of 4
spaces (as is the traditional python
indentation apparently), and apart from that, applied successfully only
point 6 of your suggestion list, and
surprisingly now get the following !:

[lxplus096-14:50:04] 0:a/ad/scripts> brush na49i01 'ps -ef | grep syslog'
===== NA49I01 OUTPUT ===============================>
root     31795     1  0 03:01 ?        00:00:05 syslogd -m 0

[lxplus096-14:50:12] 0:a/ad/scripts> brush na49i01 'ps -ef | grep zzzzzzz'
===== NA49I01 OUTPUT ===============================>
None     <<< !!!
[lxplus096-14:50:22] 0:a/ad/scripts>

How about that then ? I still don't understand how this works; I thought
before that the break was
causing the script to barf  for some reason, but now if the indents are
correct, a "None" is being thrown out
instead when the break kicks in due to the 0 line output, which is better or
at least ok for the
moment, I suppose.

BTW is "None" a response built into Python processing that can appear in
certain situations such as this?

further comments follow for the points I did try addressing ...

"John Hunter" <>

2)
>        while 1:
>           line = output.readlines()
>           if not line: break                            <<<<<<<    is this
>
>    This is confusing to me.  The typical idiom is
>
>    while 1:
>       line = output.readline()   #reads only one line, not all
>       on_some_condition(): break
>
>    But in your case, you just want to return all the output, so
>    output.read() is probably what you want.

output.read() instead of output.readlines plants a space inbetween every
character of
my output, although I'm sure there's a way of fixing that fairly smoothly.

> 3) To test against the empty string, you may want to consider
>
>    if len(line.strip())==0
>
>   strip removes spaces and newlines

more study required :=)

> 4) command_response = string.join(line[:]).  See 1
>
> 5) node_in_caps = string.upper(machine)
>
>    node_in_caps = machine.upper()

this didntt work - python said: "machine does not have an 'upper' attribute"

> 6) if listening_status == 1:
>
>   can be simplified to 'if listening_status:'
>
> Here, then, is a version of the script with these changes:
>
>   import os, sys
>   def the_command():
>       payload = ' '.join(sys.argv[1:])
>       sshcmd = 'ssh -1 -o StrictHostKeyChecking=no -l root ' + payload
>       output = os.popen('{ ' + sshcmd + '; } 2>&1', 'r')
>       return output.read()
>
>   machine = sys.argv[1]
>   node_in_caps = machine.upper()
>
>   print node_in_caps, the_command()
>
> Note if you are feeling exceptionally pithy, you are perfectly welcome
> to shorten 'the_command' to the one-liner
>
> def the_command():
>     return os.popen('{ ssh -1 -o StrictHostKeyChecking=no -l root %s; }
2>&1' %
>     ' '.join(sys.argv[1:])).read()
>
> Whether this is readable and good python style is debatable.

to say the least ! =)

I'm happy this works in a satisfactory fashion, but it's probably time I did
the python tutorial
step by step and really bone up on the basic principles before attempting
anything more serious or
elaborate.

thanks again,

AD






More information about the Python-list mailing list