如何使用 python 带有密码而不是密钥文件的 sshtunnel
How to use python sshtunnel with password instead of key file
我想从我的本地计算机打开一个 ssh 隧道,以将我的 python 脚本连接到远程数据库。
我用来打开ssh隧道的代码是:
with sshtunnel.SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_user,
ssh_password=ssh_password,
remote_bind_address=(db_ip, db_port)
) as tunnel:
# dummy code to check tunnel
tunnel.start()
logger.debug(f"IsUp: {tunnel.tunnel_is_up} | {tunnel.local_bind_address}")
我希望这样做(从各种来源做完全相同的事情)是使用 ss_user 和 ssh_password 作为凭据打开一个 ssh 隧道。 (是的,当我手动 ssh 到 shell 中的服务器时,凭据正在工作)
我得到的是来自本地系统 (Ubuntu 20.04) 的提示,要求解锁我的个人 ssh 密钥,尽管我明确配置了 SSHTunnelForwarder 以使用密码。
然后我得到的错误消息是无法建立 ssh 隧道,因为使用 ssh 密钥的身份验证失败:
2021-11-23 08:40:10,847| ERROR | Password is required for key <redacted path>/.ssh/id_rsa
2021-11-23 08:46:25,737| ERROR | Exception: key cannot be used for signing
2021-11-23 08:46:25,786| ERROR | Traceback (most recent call last):
2021-11-23 08:46:25,786| ERROR | File "<redacted path>/lib/python3.9/site-packages/paramiko/transport.py", line 2109, in run
2021-11-23 08:46:25,786| ERROR | handler(self.auth_handler, m)
2021-11-23 08:46:25,786| ERROR | File "<redacted path>/lib/python3.9/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
2021-11-23 08:46:25,786| ERROR | sig = self.private_key.sign_ssh_data(blob)
2021-11-23 08:46:25,786| ERROR | File "<redacted path>/lib/python3.9/site-packages/paramiko/agent.py", line 418, in sign_ssh_data
2021-11-23 08:46:25,787| ERROR | raise SSHException("key cannot be used for signing")
2021-11-23 08:46:25,787| ERROR | paramiko.ssh_exception.SSHException: key cannot be used for signing
2021-11-23 08:46:25,787| ERROR |
2021-11-23 08:46:25,787| ERROR | Could not connect to gateway <ssh_host>:<ssh_port> : key cannot be used for signing
Traceback (most recent call last):
File "<redacted path>/lib/python3.9/site-packages/sshtunnel.py", line 1608, in __enter__
self.start()
File "<redacted path>/lib/python3.9/site-packages/sshtunnel.py", line 1331, in start
self._raise(BaseSSHTunnelForwarderError,
File "<redacted path>/lib/python3.9/site-packages/sshtunnel.py", line 1174, in _raise
raise exception(reason)
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway
python-BaseException
Process finished with exit code 1
默认情况下,sshtunnel 以某种方式尝试加载标准 .ssh 路径中的任何 ssh 密钥,而不是使用提供的凭据。
这是lib版本中的错误吗?
我正在使用
paramiko==2.8.0
sshtunnel==0.4.0
或者我必须更改我的设置吗?
非常感谢任何帮助。
如 document 中所述,将 allow_agent
设置为 False
将帮助您 Enable/disable load of keys from an SSH agent
。例如:
sshtunnel.SSHTunnelForwarder(..., allow_agent=False)
我想从我的本地计算机打开一个 ssh 隧道,以将我的 python 脚本连接到远程数据库。 我用来打开ssh隧道的代码是:
with sshtunnel.SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_user,
ssh_password=ssh_password,
remote_bind_address=(db_ip, db_port)
) as tunnel:
# dummy code to check tunnel
tunnel.start()
logger.debug(f"IsUp: {tunnel.tunnel_is_up} | {tunnel.local_bind_address}")
我希望这样做(从各种来源做完全相同的事情)是使用 ss_user 和 ssh_password 作为凭据打开一个 ssh 隧道。 (是的,当我手动 ssh 到 shell 中的服务器时,凭据正在工作)
我得到的是来自本地系统 (Ubuntu 20.04) 的提示,要求解锁我的个人 ssh 密钥,尽管我明确配置了 SSHTunnelForwarder 以使用密码。
然后我得到的错误消息是无法建立 ssh 隧道,因为使用 ssh 密钥的身份验证失败:
2021-11-23 08:40:10,847| ERROR | Password is required for key <redacted path>/.ssh/id_rsa
2021-11-23 08:46:25,737| ERROR | Exception: key cannot be used for signing
2021-11-23 08:46:25,786| ERROR | Traceback (most recent call last):
2021-11-23 08:46:25,786| ERROR | File "<redacted path>/lib/python3.9/site-packages/paramiko/transport.py", line 2109, in run
2021-11-23 08:46:25,786| ERROR | handler(self.auth_handler, m)
2021-11-23 08:46:25,786| ERROR | File "<redacted path>/lib/python3.9/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
2021-11-23 08:46:25,786| ERROR | sig = self.private_key.sign_ssh_data(blob)
2021-11-23 08:46:25,786| ERROR | File "<redacted path>/lib/python3.9/site-packages/paramiko/agent.py", line 418, in sign_ssh_data
2021-11-23 08:46:25,787| ERROR | raise SSHException("key cannot be used for signing")
2021-11-23 08:46:25,787| ERROR | paramiko.ssh_exception.SSHException: key cannot be used for signing
2021-11-23 08:46:25,787| ERROR |
2021-11-23 08:46:25,787| ERROR | Could not connect to gateway <ssh_host>:<ssh_port> : key cannot be used for signing
Traceback (most recent call last):
File "<redacted path>/lib/python3.9/site-packages/sshtunnel.py", line 1608, in __enter__
self.start()
File "<redacted path>/lib/python3.9/site-packages/sshtunnel.py", line 1331, in start
self._raise(BaseSSHTunnelForwarderError,
File "<redacted path>/lib/python3.9/site-packages/sshtunnel.py", line 1174, in _raise
raise exception(reason)
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway
python-BaseException
Process finished with exit code 1
默认情况下,sshtunnel 以某种方式尝试加载标准 .ssh 路径中的任何 ssh 密钥,而不是使用提供的凭据。 这是lib版本中的错误吗? 我正在使用
paramiko==2.8.0
sshtunnel==0.4.0
或者我必须更改我的设置吗? 非常感谢任何帮助。
如 document 中所述,将 allow_agent
设置为 False
将帮助您 Enable/disable load of keys from an SSH agent
。例如:
sshtunnel.SSHTunnelForwarder(..., allow_agent=False)