使用 Paramiko 的 GCP 上的 SSH 连接超时

发布于 2025-01-10 13:42:02 字数 1625 浏览 0 评论 0原文

我正在尝试使用 Cloud Composer 中的 paramiko 通过 SFTP 将文件从 GCP 推送到外部服务器。下面是代码示例:

    import paramiko

    host, port = "hostname", 22
    transport = paramiko.Transport((host, port))
    username, password = "username", "password"

    transport.banner_timeout = 100

    transport.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(transport)

    if sftp:
        sftp.close()

    if transport:
        transport.close()

当我在 GCP 上运行此代码时,出现以下错误

raise socket.timeout()\nsocket.timeout\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File "/tmp/venvuuscmldg/script.py", line 25, in <module>\n    res = make_sftp_connectors(*args, **kwargs)\n  File "/tmp/venvuuscmldg/script.py", line 16, in make_sftp_connectors\n    transport.connect(username=username, password=password)\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 1178, in connect\n    self.start_client()\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 550, in start_client\n    raise e\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 1904, in run\n    self._check_banner()\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 2054, in _check_banner\n    \'Error reading SSH protocol banner\' + str(e)\nparamiko.ssh_exception.SSHException: Error reading SSH protocol banner\n'

我尝试增加超时,但没有任何变化,看起来这是 GCP 和外部服务器之间的连接问题。是否是由于默认情况下 GCP 的出口连接受到限制(例如 VPC、防火墙规则)?

I am trying to push a file via SFTP from GCP to an external server using paramiko in Cloud Composer. Below is a sample of the code:

    import paramiko

    host, port = "hostname", 22
    transport = paramiko.Transport((host, port))
    username, password = "username", "password"

    transport.banner_timeout = 100

    transport.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(transport)

    if sftp:
        sftp.close()

    if transport:
        transport.close()

When I run this code on GCP I get the following error

raise socket.timeout()\nsocket.timeout\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File "/tmp/venvuuscmldg/script.py", line 25, in <module>\n    res = make_sftp_connectors(*args, **kwargs)\n  File "/tmp/venvuuscmldg/script.py", line 16, in make_sftp_connectors\n    transport.connect(username=username, password=password)\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 1178, in connect\n    self.start_client()\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 550, in start_client\n    raise e\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 1904, in run\n    self._check_banner()\n  File "/tmp/venvuuscmldg/lib/python3.6/site-packages/paramiko/transport.py", line 2054, in _check_banner\n    \'Error reading SSH protocol banner\' + str(e)\nparamiko.ssh_exception.SSHException: Error reading SSH protocol banner\n'

I have tried increasing the timeout but nothing changes and it looks like it is a connection issue between GCP and the external server. Is it due to restrictions in egress connections from GCP (e.g. VPC, Firewall rules) by default?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2025-01-17 13:42:02

原来是GCP中有一条防火墙规则禁止连接到外部服务器的IP地址。

It turned out it was there was a firewall rule in the GCP that prohibited connecting to the external server's IP address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文