简体   繁体   English

使用Python捕获sshtunnel异常

[英]Catch sshtunnel exception with Python

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. 我有一个python代码,它建立了到远程服务器的SSH连接,以将telnet流量进一步转发到隐藏在该服务器后面的多个路由器,以便对它们进行远程管理。 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 ... ). 当由于某种原因(超时,没有可用的路由等)无法访问远程路由器(换句话说, 127.0.0.1:self.localPort -> self.systemIP, 23的组合)时,就会出现问题。

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 (ie: if error -> quit() ) 即使错误是正确的(远程路由器实际上没有可达性),我也无法捕捉到该错误:python程序永远卡在这里,而且我无法正确退出(例如: if error -> quit()

Do you have any clue on how to accomplish this? 您对如何实现这一目标有任何了解吗?

Thanks! 谢谢!

Lucas 卢卡斯

So, the issue of catching the exception was solved at a later stage within the code. 因此,在代码的稍后阶段解决了捕获异常的问题。

After triggering the telnet connection ... 触发telnet连接后...

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 由于某种原因,我认为包括try | except try | except when creating the tn connection was enough. try | except创建tn连接时已足够。 But no, I kept on receiving the SSH error mentioned. 但是,不,我一直收到提到的SSH错误。

Wrapping the tn.expect with try|except did the trick. try|except包裹tn.expect达到目的。 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 卢卡斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM