def connect(self, host, port=None, through=None): '''establish a secure shell tunnel between local and remote host Input: host -- remote hostname [user@host:path is also valid] port -- remote port number Additional Input: through -- 'tunnel-through' hostname [default = None] ''' from pathos.portpicker import portnumber if port is None: from pathos.core import randomport port = randomport(through) if through else randomport(host) pick = portnumber(self.MINPORT, self.MAXPORT) while True: localport = pick() if localport < 0: raise TunnelException, 'No available local port' #print 'Trying port %d...' % localport try: self._connect(localport, host, port, through=through) #print 'SSH tunnel %d:%s:%d' % (localport, host, port) except TunnelException, e: if e.args[0] == 'bind': self.disconnect() continue else: self.__disconnect() raise TunnelException, 'Connection failed' self.connected = True return localport