使用 Paramiko 的 GCP 上的 SSH 连接超时
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原来是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.