SSHTunnelForwarder-“ IP不是字符串(NoneType)”

时间:2018-08-24 23:14:30

标签: python python-3.x ssh ssh-tunnel

我正在尝试使用SSHTunnelForwarder。我收到错误“ IP不是字符串(NoneType)”。如果我正确理解,则意味着我要传递的主机变量的类型为NoneType,并且必须为String类型。打印出变量类型表明它的类型为字符串:

我已经将代码泛化了。见下文:

def connectToClient(client):
    key, host = getClientInfo(client)
    # Both of the below print statements return <class 'str'>
    print(type(key))
    print(type(host))

    try:
        with SSHTunnelForwarder(
            ssh_username = "username",
            ssh_private_key = key,
            remote_bind_address = (host, 22)) as server:
                # Do stuff here

    except Exception as e:
        # Handle exception

堆栈跟踪(已编辑以隐藏文件系统信息和.py文件名。还使用0.0.0.0进行测试):

Traceback (most recent call last):
  File "/path/to/file/myPythonFile.py", line 83, in connectToClient
    remote_bind_address = ('0.0.0.0', 22)) as server:
  File "/path/to/file/lib/python3.6/site-packages/sshtunnel.py", line 908, in __init__
    check_host(self.ssh_host)
  File "/path/to/file/lib/python3.6/site-packages/sshtunnel.py", line 79, in check_host
    type(host).__name__
AssertionError: IP is not a string (NoneType)

我想指出的是,即使我用包含IP的字符串(即“ 127.0.0.1”)替换了主机变量,我仍然会收到相同的错误。

也许我在犯一个简单的错误。有什么想法吗?

编辑:修复格式并添加堆栈跟踪

1 个答案:

答案 0 :(得分:2)

您缺少争论。请参见example
实际上,您在调用SSHTunnelForwarder时没有使用关键字参数ssh_address_or_host,该参数代表远程主机的IP和端口号。
确实,您正在混淆remote_bind_address的使用含义,因为它就是ssh_address_or_host

当该方法将检查相对于IP的主机部分时,将不会找到字符串,但是None(无)和例外。参见code