1

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

Lucas Aimaretto
  • 1,399
  • 1
  • 22
  • 34
  • 1
    I have the feeling the important bit of code is still missing, as these are just 2 function definitions in which from the actual error messages shown, the exception is never raised. – Uvar Jul 24 '17 at 15:42
  • Hi @Uvar: yes, indeed. The code is long, that's why I did not post it complete. However, after looking at the code again, I found the mistake. I'll post it as an answer. Thanks! – Lucas Aimaretto Jul 24 '17 at 16:49

1 Answers1

1

So, the issue of catching the exception was solved at a later stage within the code.

After triggering the telnet connection ...

def routerLogin(self):
    try:
        self.tn = telnetlib.Telnet("127.0.0.1",self.localPort)
    except:
        fncPrintConsole(self.strConn + "No route to host!")
        self.quit()

... I do expect some string before going on.

i = self.tn.expect(PROMPT_LOGIN, TIME_LOGIN)

For some reason I thought that including try | except when creating the tn connection was enough. But no, I kept on receiving the SSH error mentioned.

Wrapping the tn.expect with try|except did the trick. So now I have...

        try:
            i = self.tn.expect(PROMPT_LOGIN, TIME_LOGIN)
        except:
            quit()

... and in the case of reachability problems I can catch it up there.

Don't know if this is the more elegant / right way of doing it but at least is working ...

Thanks!

Lucas

Lucas Aimaretto
  • 1,399
  • 1
  • 22
  • 34