【问题标题】:Paramiko [Errno None] Unable to connect to port 22 on 172.16.127 Using Pycharm and tkinterParamiko [Errno None] 使用 Pycharm 和 tkinter 无法连接到 172.16.127 上的端口 22
【发布时间】:2020-06-23 13:40:15
【问题描述】:

我正在使用 tkinter 构建桌面应用程序。此外,使用 Paramiko 进行 ssh 连接。该应用程序有一个“连接”按钮调用该函数

def checkAuth():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, 22, username=userName, password=password)

这是 tkinter 应用程序中对函数的调用。

"Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\****\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/****/PycharmProjects/tracer/tracer.py", line 18, in checkAuth
ssh.connect(host, 22, username=userName, password=password)
File "C:\Users\****\PycharmProjects\tracer\venv\lib\site-packages\paramiko\client.py", line 368, in connect
raise NoValidConnectionsError(errors)
paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 22 on 172.16.127.6 or fe80::d99d:ff15:3fc1:482e

connectButton = Button(connect, text="Connect", width=6, command=checkAuth)

但是得到“paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 22 on 172.16.127.6”作为输出。”

我可以将这个函数作为一个备用脚本运行,并且它连接良好。我可以通过 cmd 和 putty SSH 到主机没有问题。似乎只有在 tkinter 应用程序中使用该功能时才会遇到这种情况。

非常感谢所有帮助。

【问题讨论】:

  • 主机 ip 看起来不对,是错字吗?
  • IP地址错误。 IP 地址由四个部分组成,而不是三个部分。
  • 我很抱歉。这是一个错字!我已经编辑了原始帖子。 “.6”不见了。
  • 在您的 tkinter 应用程序中调用它的位置是什么?你能把它的代码贴出来吗?
  • 谢谢@noobius。我已重新编辑,包括调用此函数的按钮命令。

标签: python tkinter ssh pycharm paramiko


【解决方案1】:

这是我如何通过 ssh 连接到远程机器的示例:

import tkinter as tk
import os
import paramiko

def setup_ssh_client(host):
    print("establishing ssh connection....")
    client = paramiko.client.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    home = os.path.expanduser("~")
    ret = os.path.exists(os.path.join(home, "somekey.pem"))
    if not ret:
        print("Unable to locate pem key, upload aborted....")
        return None

    private_key = paramiko.RSAKey.from_private_key_file(os.path.join(home, "somekey.pem"))
    try:
        client.connect(hostname=host, username="bob", pkey=private_key)
    except Exception as e:
        print(e)
    print("connected")
    stdin, stdout, stderr = client.exec_command('ls')
    for line in stdout:
        print('... ' + line.strip('\n'))
    return client

window = tk.Tk()
connectButton = tk.Button(window, text="Connect", width=6, command=setup_ssh_client)
connectButton.pack()
window.mainloop()

我用 tkinter 试过这个,当点击按钮时它连接正常。我没有看到与您正在做的事情有太大不同,也许添加 client.load_system_host_keys() 并查看它是否有帮助。只需使用您的用户名和密码而不是私钥来进行身份验证,而不是我在示例中所做的。

【讨论】:

  • 不幸的是,添加 client.load_system_host_keys() 没有运气。不确定我是否理解您关于不使用私钥的评论。你能解释一下吗?
  • @EricPraline 您可以使用用户名和密码进行身份验证,或者如果您使用 ssh 服务器设置公钥/私钥,那么您可以使用您的私钥向 ssh 服务器进行身份验证(服务器有您的公钥并将使用您的私钥进行加密/解密进行身份验证)。这是另一种身份验证机制。
  • @EricPraline 我更新了我的完整示例代码,如果你尝试了但仍然无法连接,那么可能是其他东西阻止了连接。如果可能,尝试使用不同的主机,看看主机是否仍在运行,并且您仍然可以从 putty SSH 进入。仍然没有意义,它可以在独立脚本中工作(没有 tkinter)。
  • 我对私钥的问题是,这个应用程序旨在让用户登录到许多不同的 ssh 服务器,而不是每次都登录同一个。奇怪的是它在 tkinter 之外工作。非常感谢您花费的时间。
  • 如果有帮助的话,我也设法让 tikinter 回电。我已经更新了原始帖子以包含它。
猜你喜欢
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 2017-12-01
  • 2020-05-11
  • 2016-04-17
相关资源
最近更新 更多