【发布时间】:2021-07-26 17:48:52
【问题描述】:
我是 python 新手。我正在尝试使用 python 连接到 mysql。这是下面的代码:
import mysql.connector
import sshtunnel
with sshtunnel.SSHTunnelForwarder(
ssh_username="ravi",
ssh_address="maxim.com",
ssh_pkey="~/.ssh/id_rsa_host",
remote_bind_address=("127.0.0.1", 3306),
) as tunnel:
connection = mysql.connector.connect(
user="power_pack",
password="987654",
host="db.maxim.com",
port=3306,
)
我收到以下错误:
Traceback (most recent call last):
File "/Users/ashok/Documents/Repos/db-migration/src/index.py", line 4, in <module>
with sshtunnel.SSHTunnelForwarder(
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/sshtunnel.py", line 966, in __init__
(self.ssh_password, self.ssh_pkeys) = self._consolidate_auth(
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/sshtunnel.py", line 1148, in _consolidate_auth
ssh_loaded_pkeys = SSHTunnelForwarder.get_keys(
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/sshtunnel.py", line 1105, in get_keys
ssh_pkey = SSHTunnelForwarder.read_private_key_file(
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/sshtunnel.py", line 1304, in read_private_key_file
ssh_pkey = pkey_class.from_private_key_file(
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/paramiko/pkey.py", line 235, in from_private_key_file
key = cls(filename=filename, password=password)
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/paramiko/rsakey.py", line 55, in __init__
self._from_private_key_file(filename, password)
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/paramiko/rsakey.py", line 175, in _from_private_key_file
data = self._read_private_key_file("RSA", filename, password)
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/paramiko/pkey.py", line 308, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File "/Users/ashok/Library/Python/3.9/lib/python/site-packages/paramiko/pkey.py", line 316, in _read_private_key
m = self.BEGIN_TAG.match(lines[start])
IndexError: list index out of range
同时将密钥传递给 paramiko 可以正确解析文件,
paramiko.RSAKey.from_private_key_file("~/.ssh/id_rsa_host")
【问题讨论】:
-
您的私钥文件似乎为空或不完整,因为错误来自
paramiko包尝试读取密钥时。你可以使用相同的文件在终端中启动 SSH 会话吗? -
是的,它在终端中运行良好
-
同样使用 Paramiko 文件私钥被正确解析。但是当我通过 ssh 隧道时它会中断。
标签: python ssh paramiko ssh-tunnel