Discussion:
Handling of badly behaved server
Giles Brown
2010-08-12 13:28:13 UTC
Permalink
Hi all,



I'm new to using paramiko. I'm trying to get paramiko to give up when it
encounters a badly behaved server (see below). Can anyone give me a tip on
how to do this and whether it is possible?



The client is running on Windows and so is the server. I've tried this with
1.7.6 under Python 2.6 and 1.7.1 under Python 2.3 with the same result in
both cases.



Thanks.



Cheers,

Giles



testhang.py

"""

import paramiko



ssh = paramiko.SSHClient()

# This hangs...

ssh.connect('127.0.0.1', username='dontcare', password='dontcare',
timeout=10.0)

"""



"""



badserver.py

"""

import sys

import time

import SocketServer



class BadHandler(SocketServer.BaseRequestHandler):

""" Demonstrate bad SSH server behaviour.

"""



def handle(self):

self.request.send('SSH-1.99-USHA SSHv0.1\n')

# Uh-oh. I'm going to wait here forever!

try:

time.sleep(1000000)

except KeyboardInterrupt:

pass



if __name__ == "__main__":

server = SocketServer.TCPServer(('127.0.0.1', 22), BadHandler)

server.handle_request()

"""
james bardin
2010-08-12 19:56:26 UTC
Permalink
I’m new to using paramiko.  I’m trying to get paramiko to give up when it
encounters a badly behaved server (see below).  Can anyone give me a tip on
how to do this and whether it is possible?
...
The client is running on Windows and so is the server.  I’ve tried this with
1.7.6 under Python 2.6 and 1.7.1 under Python 2.3 with the same result in
both cases.
Since the server is at fault, the only way around it is to timeout on
the client side, and kill the connection.
At a high level, you could use a threading.Timer around ssh.connect()
to cancel the negotiation. Your handler function calling close() on
the SSHClient object should be sufficient here, which would raise an
SSHException. If you're working at a lower level, you also have the
choice of giving transport.start_client() a threading.Event to work
asynchronously.


-jim
--
James Bardin <***@bu.edu>
Systems Engineer
Boston University IS&T
Loading...