与 Paramiko 的无密码 SSH 连接失败,而 SSH 可以正常连接

2024-03-17

我打算在客户端和服务器之间使用 SSH 密钥创建无密码连接。

使用 paramiko,我最终得到了认证异常。通过 Popen 使用标准 SSH,我可以毫无问题地连接

对于 Paramiko,我使用以下代码:

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
client.connect(ssh_server, username=ssh_user)

对于同样的情况,我可以使用 SSH:

cmd = 'ssh -o GSSAPIAuthentication=no -o ForwardX11=no {}@{} echo 0 > /dev/null'.format(ssh_user, ssh_server)
process = subprocess.Popen(md, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()

我正在使用 Python 3.4.6 和 Paramiko 2.4.1。

更多细节:

  • 我能够在不同的客户端上与 Paramiko 建立连接
  • 在此特定客户端上,ssh 模拟的用户与发起呼叫的用户不同。

Paramiko 调试输出

2018-07-11 20:55:54,025 20730 DEBUG: putting paramiko output to console
2018-07-11 20:55:54,112 20730 DEBUG: starting thread (client mode): 0x4ca07320
2018-07-11 20:55:54,112 20730 DEBUG: Local version/idstring: SSH-2.0-paramiko_2.4.1
2018-07-11 20:55:54,112 20730 DEBUG: Remote version/idstring: SSH-2.0-OpenSSH_7.2
2018-07-11 20:55:54,112 20730 INFO: Connected (version 2.0, client OpenSSH_7.2)
2018-07-11 20:55:54,115 20730 DEBUG: kex algos:['[email protected] /cdn-cgi/l/email-protection', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group14-sha1'] server key:['ssh-rsa', 'rsa-sha2-512', 'rsa-sha2-256', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ssh-ed25519'] client encrypt:['[email protected] /cdn-cgi/l/email-protection', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection'] server encrypt:['[email protected] /cdn-cgi/l/email-protection', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection'] client mac:['[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] server mac:['[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] client compress:['none', '[email protected] /cdn-cgi/l/email-protection'] server compress:['none', '[email protected] /cdn-cgi/l/email-protection'] client lang:[''] server lang:[''] kex follows?False
2018-07-11 20:55:54,115 20730 DEBUG: Kex agreed: ecdh-sha2-nistp256
2018-07-11 20:55:54,115 20730 DEBUG: HostKey agreed: ecdsa-sha2-nistp256
2018-07-11 20:55:54,115 20730 DEBUG: Cipher agreed: aes128-ctr
2018-07-11 20:55:54,115 20730 DEBUG: MAC agreed: hmac-sha2-256
2018-07-11 20:55:54,116 20730 DEBUG: Compression agreed: none
2018-07-11 20:55:54,123 20730 DEBUG: kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
2018-07-11 20:55:54,124 20730 DEBUG: Switch to new keys ...
Exception: No authentication methods available

SSH 调试:(使用 ssh -v ssh_user AT ssh_server)

OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /home/pid1083/.ssh/config
debug1: /home/pid1083/.ssh/config line 17: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to <ssh_server> port 22.
debug1: Connection established.
debug1: identity file /home/<logged_in_username>/.ssh/id_rsa_c2c type 1
debug1: identity file /home/<logged_in_username>/.ssh/id_rsa_c2c-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.10
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2
debug1: match: OpenSSH_7.2 pat OpenSSH* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr [email protected] /cdn-cgi/l/email-protection none
debug1: kex: client->server aes128-ctr [email protected] /cdn-cgi/l/email-protection none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 21:1b:bb:32:4c:69:f3:eb:91:79:e7:ca:d4:30:ff:70
debug1: Host '<ssh_server>' is known and matches the ECDSA host key.
debug1: Found key in /home/<logged_in_username>/.ssh/known_hosts:519
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available

debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available

debug1: Unspecified GSS failure.  Minor code may provide more information


debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/<logged_in_user>/.ssh/id_rsa_c2c
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to <IP_of_server>:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected] /cdn-cgi/l/email-protection
debug1: Entering interactive session.
debug1: client_input_global_request: rtype [email protected] /cdn-cgi/l/email-protection want_reply 0
debug1: Requesting X11 forwarding with authentication spoofing.
debug1: Sending environment.
debug1: Sending env LC_PAPER = en_US.utf8
debug1: Sending env LC_ADDRESS = en_US.utf8
debug1: Sending env LC_MONETARY = en_US.utf8
debug1: Sending env LC_NUMERIC = en_US.utf8
debug1: Sending env LC_ALL = en_US.utf8
debug1: Sending env LC_TELEPHONE = en_US.utf8
debug1: Sending env LC_MESSAGES = en_US.utf8
debug1: Sending env LC_IDENTIFICATION = en_US.utf8
debug1: Sending env LC_COLLATE = en_US.utf8
debug1: Sending env LANG = en_US.utf8
debug1: Sending env LC_MEASUREMENT = en_US.utf8
debug1: Sending env LC_CTYPE = en_US.utf8
debug1: Sending env LC_TIME = en_DK.utf8
debug1: Sending env LC_NAME = en_US.utf8

Your ssh连接有效,因为它使用来自~/.ssh/id_rsa_c2c(如配置中ssh_config). The .ssh文件夹和ssh_config文件用作 OpenSSH 工具集的配置(ssh在这种情况下)。您不能期望其他 SSH 客户端/库将使用 OpenSSH 配置文件。一般来说,他们不会。

如果您想对 Paramiko 使用公钥身份验证,请使用key_filename的论证SSHClient.connect https://docs.paramiko.org/en/latest/api/client.html#paramiko.client.SSHClient.connect.
也可以看看如何使用 ppk 公钥通过 Python Paramiko 进行 ssh 连接 https://stackoverflow.com/q/8382847/850848.

尽管 Paramiko 特别会使用密钥文件,但如果它有一个通用名称,例如id_rsa, id_dsa等参见在 Python 中的 Paramiko 中强制进行密码身份验证(忽略 .ssh 文件夹中的密钥) https://stackoverflow.com/q/52632693/850848对于完全相反的问题。


强制性警告:请勿使用AutoAddPolicy,除非你不关心安全性。通过这种方式,您将失去针对 MITM 攻击的保护。
正确的解决方案请参见Paramiko“未知服务器” https://stackoverflow.com/q/10670217/850848#43093883
.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

与 Paramiko 的无密码 SSH 连接失败,而 SSH 可以正常连接 的相关文章

随机推荐

  • 如何将 Access 2003 mde 与 Access 2007 一起使用并保留自定义菜单/工具栏?

    请描述在 Access 2007 运行时中使用已编译的 Access 2003 mdb 所需的确切步骤 并保留我的自定义菜单和工具栏 以便它们看起来与在 2003 运行时中一样 我尝试遵循网络上的各种不完整的说明 但最终我的菜单 工具栏要么
  • 如何找到以编程方式编译 .NET 程序集的平台?

    看看下面的JetBrains dotPeek鉴于某些给定的程序集 我如何以编程方式确定此类程序集编译的平台 我尝试了以下方法 该方法不适用于在 NET Core 中编译的程序集 public static string GetFramewo
  • iPhone 上的色彩平衡

    我正在拍摄一张图像 通过屏幕上下文加载它 并逐像素地更改它 我有许多不同的滤镜应用于图像 但我需要做的最后一件事是改变色彩平衡 类似于 Photoshop 以使红色更加青色 下面的代码显示了我如何拍摄图像 获取数据以及逐像素检查 r g b
  • 使用 hash_hmac 和ripemd160 快速散列字符串

    我正在尝试使用 Swift 3 和 或 Objective C 对字符串进行哈希处理ripemd160模拟以下 php 的输出 string string key test hash hmac ripemd160 string key ou
  • 在 PowerPoint 中重命名对象

    可能是一个非常愚蠢的问题 但我不知道如何在 PowerPoint 中重命名对象 例如 我所有的图表默认称为 图表 1 等 有人可以帮我吗 谢谢 在 PowerPoint 2007 中 您可以从 选择 窗格执行此操作 要显示选择窗格 请单击功
  • 尽管在本地主机上工作,但部署到 Azure 时,Azure Active Directory 始终重定向到“~/.auth/login/done”

    因此 我正在开发一个 ASP NET Core 应用程序 NET Core 2 0 作为 Azure 上的应用服务托管 我想使用单个租户 因此只有我们公司的帐户 通过 Azure AD 实施身份验证 实际上 我添加了所有必要的代码 注册了应
  • 在 Outlook 中执行搜索查询

    您好 我想知道是否可以从我的 WinForms 应用程序向 Outlook 2010 发送搜索查询 也就是说 不是像我一直在搜索并找到的那样搜索 PST 文件 而是尝试在 Outlook 中显示结果列表 就像我自己在搜索框中键入内容一样 如
  • ELM QueryString 解析器无法编译

    我真的很想学习一些 ELM 但是我的思想在查询解析时崩溃了 我的想法是创建一个函数来按名称获取查询字符串值 例如 给定一个查询字符串 name Neuber像这样的函数getParam name 那会返回Neuber 但它在最基本的例子中失
  • wxpython滚动面板不更新滚动条

    我正在使用 winxp 和 wxpython wxpython 3 1 python 2 6 制作一个 GUI 程序 它将文本从 TextCtrl 复制到包含 StaticText 的 ScrollablePanel 中 这一切都工作正常
  • 在内容安全策略中将多个域列入白名单

    我正在编写一个 chrome 扩展 它的白名单中需要有两个域以用于内容安全策略 我已经查看了官方文档 但似乎仍然无法弄清楚正确的语法 以下似乎不起作用 content security policy script src self http
  • AWS NodeJS SDK V3 DynamoDB UpdateItem - TypeError:无法读取未定义的属性“0”

    我正在尝试使用新的适用于 NodeJS 的 AWS SDK V3 在 NodeJS 中进行基本的数据库更新操作 我尝试更新的数据对象如下所示 auth BOOL false username S siegbert secondsLeft N
  • 如何查看和复制 R 默认 Makevars 配置?

    我没有 R Makevars文件和 gt tools makevars user character 0 gt tools makevars site character 0 Yet R必须从某处读取配置 cpp文件包含Rcpp导出到包中s
  • 计算公式的子公式

    选择有公式的单元格后 我们可以在Excel公式栏中选择其公式的一部分并进行计算 按F9 我需要通过 JavaScript API 重现子公式的计算 例如 假设 Cell 的值A1 B1 C1分别是1 2 3 和细胞D1包含一个公式 A1 B
  • 将 2d int 数组转换为 2d double 的最短方法

    我有一个方法只能得到double 我想传递给int 在java中有没有一种简单的方法可以做到这一点 就像这样简单 int iArray 1 2 5 6 double dArray double iArray 不幸的是 转换数组的唯一方法是迭
  • 使声明的名称空间正常工作时遇到问题

    我试图获取用外部 css 编写的命名空间 实际上是两个单独的文件 当我在浏览器上运行该文件时 它不会使用声明的名称空间 我认为该文件正在调用 css 文件 但它没有应用命名空间样式 HTML
  • Fortran 接受来自 C 的字符串(?)

    我觉得这应该是一个简单的问题 但我无法让它发挥作用 我有一些 Fortran 代码 需要如下输入 SUBROUTINE TRACE X Y NAME XX YY EXTERNAL NAME CALL NAME X Y XX YY 我正在尝试
  • ILookup 不应该在 TElement 中(声明)协变吗?

    定义System Linq ILookUp
  • 在两个不同的系统之间同步对象,最好的方法是什么?

    我正在致力于使用基于 XML 的有效负载在 iPhone 和网站之间同步两个业务对象 并且很乐意征求一些关于最佳例程的想法 这个问题的本质是相当通用的 我可以看到它适用于需要在 Web 实体和客户端 桌面 移动电话等 之间同步业务对象的各种
  • 我什么时候会在 ASP.NET 中使用委托?

    我一直在寻找一种方法来使用我能使用的所有工具 并超越我目前的水平 但是 尽管我读过有关委托的内容 但我永远找不到使用它们的地方 例如接口 泛型和很多东西 但我离题了 我希望有人可以向我展示他们何时以及如何使用委托在webasp net c
  • 与 Paramiko 的无密码 SSH 连接失败,而 SSH 可以正常连接

    我打算在客户端和服务器之间使用 SSH 密钥创建无密码连接 使用 paramiko 我最终得到了认证异常 通过 Popen 使用标准 SSH 我可以毫无问题地连接 对于 Paramiko 我使用以下代码 client paramiko SS