【问题标题】:python sshtunnel: I get the following error: "IndexError: list index out of range"python sshtunnel:我收到以下错误:“IndexError:列表索引超出范围”
【发布时间】: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


【解决方案1】:

感谢大家的回复。

  • 这似乎是 Paramiko 的问题。 Paramiko 代码循环遍历 各种类型的 SSH 密钥。如果 第一个键类型不匹配。
  • 并且添加了一个 PR 来修复这个错误,但它仍然没有合并。一世 在本地进行更改以使其正常工作。

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2015-01-28
    相关资源
    最近更新 更多