I've got a Twisted Conch/SSH script that I inherited which I'm using to
run commands remotely on some servers.  I'd like to add a timeout for
servers that are down, etc (the script currently hangs on these), but
I&#39;m not sure where to add that functionality.<br>
<br>Any help would be appreciated.<br>Thanks,<br>-Ray<br><br>=========Script Follows=========<br><br>#!/usr/bin/python<br><br>from twisted.conch import error<br>from twisted.conch.ssh import transport, connection, keys, userauth, channel, common<br>

from twisted.internet import defer, protocol, reactor<br><br>#a class to store the data returned from the ssh client code<br>class SSHData():<br>&nbsp;&nbsp; data = &#39;&#39;<br><br>class ClientCommandTransport(<div id=":xg" class="ArwC7c ckChnd">
transport.SSHClientTransport):<br>
&nbsp;&nbsp; def __init__(self, username, password, command):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.username = username<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.password = password<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.command = command<br><br>&nbsp;&nbsp; def verifyHostKey(self, pubKey, fingerprint):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # in a real app, you should verify that the fingerprint matches<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # the one you expected to get from this server<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return defer.succeed(True)<br><br>&nbsp;&nbsp; def connectionSecure(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.requestService(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PasswordAuth(self.username, self.password,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ClientConnection(self.command)))<br>

<br>class PasswordAuth(userauth.SSHUserAuthClient):<br>&nbsp;&nbsp; def __init__(self, user, password, connection):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; userauth.SSHUserAuthClient.__init__(self, user, connection)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.password = password<br><br>&nbsp;&nbsp; def getPassword(self, prompt=None):<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return defer.succeed(self.password)<br><br>class ClientConnection(connection.SSHConnection):<br>&nbsp;&nbsp; def __init__(self, cmd, *args, **kwargs):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.SSHConnection.__init__(self)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.command = cmd<br>

<br>&nbsp;&nbsp; def serviceStarted(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.openChannel(CommandChannel(self.command, conn=self))<br><br>class CommandChannel(channel.SSHChannel):<br>&nbsp;&nbsp; name = &#39;session&#39;<br><br>&nbsp;&nbsp; def __init__(self, command, *args, **kwargs):<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; channel.SSHChannel.__init__(self, *args, **kwargs)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.command = command<br><br>&nbsp;&nbsp; def channelOpen(self, data):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.conn.sendRequest(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self, &#39;exec&#39;, common.NS(self.command), wantReply=True).addCallback(<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self._gotResponse)<br><br>&nbsp;&nbsp; def _gotResponse(self, _):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.conn.sendEOF(self)<br><br>&nbsp;&nbsp; def dataReceived(self, data):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SSHData.data = data<br><br>&nbsp;&nbsp; def closed(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reactor.stop()<br>

<br>class ClientCommandFactory(protocol.ClientFactory):<br>&nbsp;&nbsp; def __init__(self, username, password, command):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.username = username<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.password = password<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.command = command<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.deferred = defer.Deferred()<br>

<br>&nbsp;&nbsp; def buildProtocol(self, addr):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protocol = ClientCommandTransport(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.username, self.password, self.command)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return protocol<br><br>def testConnect(username, password, command, server):<br>

&nbsp;&nbsp; testFactory = ClientCommandFactory(username, password, command)<br>&nbsp;&nbsp; reactor.connectTCP(server, 22, testFactory)<br>&nbsp;&nbsp; return testFactory.deferred<br><br>def handleSuccess(result, port):<br>&nbsp;&nbsp; print &quot;Connected to port %i&quot; % port<br>

&nbsp;&nbsp; reactor.stop()<br><br>def handleFailure(failure, port):<br>&nbsp;&nbsp; print &quot;Error connecting to port %i: %s&quot; % (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; port, failure.getErrorMessage())<br>&nbsp;&nbsp; reactor.stop()<br><br>if __name__ == &quot;__main__&quot;:<br>

&nbsp;&nbsp; import sys, getpass<br><br>&nbsp;&nbsp; server = sys.argv[1]<br>&nbsp;&nbsp; command = sys.argv[2]<br>&nbsp;&nbsp; username = raw_input(&quot;Username: &quot;)<br>&nbsp;&nbsp; password = getpass.getpass(&quot;Password: &quot;)<br><br>&nbsp;&nbsp; connecting = testConnect(username, password, command, server)<br>

&nbsp;&nbsp; #connecting.addCallback(handleSuccess, server)<br>&nbsp;&nbsp; #connecting.addErrback(handleFailure, server)<br>&nbsp;&nbsp; connecting.setTimeout(1, handleError)<br>&nbsp;&nbsp; reactor.run()<br><br>&nbsp;&nbsp; print &quot;The data I got back from the ssh client is &quot; + SSHData.data</div>