timing out an ssh command

Andrei Doicin Andrei.Doicin at cern.ch
Thu May 8 05:12:53 EDT 2003


"Paul Swartz" <z3p at twistedmatrix.com> wrote in message
news:cff820f1.0305071432.3342a1cd at posting.google.com...
> I would suggest that none of those are what you want.  I'm not sure
> exactly what your requirements are, but it sounds like Conch is what
> you want.  Conch is an SSHv2 implementation using Twisted and is
> available at http://www.twistedmatrix.com/.  However, without more
> information about what you're /actually/ trying to do, I can't be
> sure.

Hi,

I've had a brief look at "twisted", and it certainly seems powerful,
although a new learning curve I'd like to avoid, in
order to stay "on the path" with Python ... so to speak.

Here's my code BTW, hacky as it may be thus far =) :

#!/usr/bin/python
#
############################################################################
#
#
# BRUSH aka WASSH-LITE
#
# BRUSH is a python script which acts as a simple wrapper more than anything
# else, allowing you to execute a command or script over ssh on a remote
host
# When possible BRUSH will use ssh-agent and RSA/DSA keys to minimize the
# need to enter your password more than once.
#
# by A. Doicin
#
# MMIII
#
############################################################################
#
#
# WARNING: the core functionality of brush is mostly reliant on SSH
#
#                                       --- Captain Obvious
#
############################################################################
#

############################################################################
#
# Function definitions
############################################################################
#

############################################################################
#
### Function "Usage"

def Usage():
    print "\nUSAGE:\n"
    print "You need to enter a hostname followed by some commands, like
this:\n"
    print "brush <hostame> <commands> <commands> ...\n"
    sys.exit(1)

############################################################################
#
### Function "keyboard_interrupt"

def keyboard_interrupt():
    print "<<<<< Keyboard Interrupt ! >>>>>\n"
    sys.exit(1)

############################################################################
#
### Function "is_it_listening"

def is_it_listening():
    ping_cmd = 'ping -c 1 -w 1 ' + machine
    ping_output = os.popen('{ ' + ping_cmd + '; } 2>&1', 'r')

   while 1:
        try:
            ping_lines = ping_output.readlines()
            if not ping_lines: break
            else:
                ping_lines_out = len(ping_lines)
                if ping_lines_out > 5:
                    status = 1
                else:
                    status = 0
            return status
        except KeyboardInterrupt: print keyboard_interrupt()

############################################################################
#
### 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:
        try:
            line = output.readlines()
            if not line: break
            else:
                command_response = string.join(line[:])
            return command_response
        except KeyboardInterrupt: print keyboard_interrupt()

############################################################################
#
#
# 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:
        print "=====", node_in_caps, "OUTPUT
===============================>\n", the_command()
    else:
        print "X ===", node_in_caps, "IS NOT CONTACTABLE ==================
X\n"
        sys.exit(1)

############################################################################

After a bit of further study, it would seem that threading is the way to go
i.e. somehow getting the pid of the ssh command itself as send by BRUSH (the
child pid ?), using some kind of Timer class based on the the "threading"
module to see if the pid is still active, and if so, after some designated
<time-out> period, killing the pid.

I have made small progress in this direction - namely with the Timer class
and threading, but the next thing is the the process control, grabbing
child-pids on the fly, etc.

At some stage, this may entail the use of os.fork, popen3 or popen4, but
exactly which of these remains to be seen.

Cheers

A.






More information about the Python-list mailing list