python sshtunnel: I get the following error: "IndexError: list index out of range"

python sshtunnel: I get the following error: "IndexError: list index out of range"

我是 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 的问题。 Paramiko代码循环通过 各种类型的 SSH 密钥。如果 第一个键类型不匹配。
  • 并且添加了一个 PR 来修复这个错误,但它仍然没有合并。我 在本地进行更改以使其正常工作。