I have a python code that brings up an SSH connection to remote server to further forward telnet traffic to several routers hidden behind this server, in order to remote manage those. The code is the following:
def sshStart(self):
try:
self.sshServer = SSHTunnelForwarder(
(SRVR_IP, SRVR_PORT),
ssh_username = SRVR_USER[0],
ssh_password = SRVR_USER[1],
remote_bind_address = (self.systemIP, 23),
local_bind_address = ("127.0.0.1", self.localPort)
)
self.sshServer.start()
except:
fncPrintConsole(self.strConn + "Error SSH Tunnel")
self.quit()
def routerLogin(self):
try:
self.tn = telnetlib.Telnet("127.0.0.1",self.localPort)
except:
fncPrintConsole(self.strConn + "No route to host!")
self.quit()
This is working very nice. Indeed, I can easily manage several routers with this code, provided that there is networking towards the far-end router.
The problem arises when the remote router (in other words, the combination of 127.0.0.1:self.localPort -> self.systemIP, 23
) is not reachable because of something (timeout, no routing available, etc ... ).
In such a case, I get the following error:
2017-07-24 10:38:57,409| ERROR | Could not establish connection from ('127.0.0.1', 50000) to remote side of the tunnel
2017-07-24 10:38:57,448| ERROR | Secsh channel 0 open FAILED: Network is unreachable: Connect failed
Even though the error is correct (there actually is no reachability to the remote router) I cannot catch that error: the python program gets stuck there for ever and I cannot exit it properly (i.e.: if error -> quit()
)
Do you have any clue on how to accomplish this?
Thanks!
Lucas