python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python SSH远程登录

Python基于PyQt5开发一个SSH远程登录终端工具

作者:爱吃香肥带鱼的胖圆圆

这篇文章主要为大家详细介绍了Python如何基于PyQt5和Paramiko开发一个SSH远程登录终端工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下

基于 PyQt5 + Paramiko 实现的 SSH 远程登录终端工具,包含服务器管理、多标签终端与远程文件浏览/下载。

效果如下:

环境要求

功能特性

服务器管理

SSH 连接

终端界面

远程文件浏览器

多标签页支持

安装依赖

pip install -r requirements.txt

快速开始(Windows)

安装依赖(见上)。

启动程序(二选一):

python RemoteTerminal_python.py

使用方法

添加服务器

连接服务器

使用终端

浏览文件

SSH 核心代码说明

连接线程:SSHConnection

命令发送与关闭

连接测试

示例核心代码:

import os
import time

import paramiko
from PyQt5.QtCore import QThread, pyqtSignal


class SSHConnection(QThread):
    """SSH 连接线程"""
    output_received = pyqtSignal(str)
    error_occurred = pyqtSignal(str)
    connection_closed = pyqtSignal()

    def __init__(self, host, port, username, password=None, key_file=None):
        super().__init__()
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.key_file = key_file
        self.client = None
        self.channel = None
        self.running = False

    def run(self):
        try:
            self.client = paramiko.SSHClient()
            self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

            # 连接服务器(密钥优先,其次密码)
            if self.key_file and os.path.exists(self.key_file):
                self.client.connect(
                    hostname=self.host,
                    port=self.port,
                    username=self.username,
                    key_filename=self.key_file,
                    timeout=10,
                )
            else:
                self.client.connect(
                    hostname=self.host,
                    port=self.port,
                    username=self.username,
                    password=self.password,
                    timeout=10,
                )

            # 创建交互式 shell
            self.channel = self.client.invoke_shell(term="xterm-256color")
            self.running = True

            # 持续读取远程输出
            while self.running:
                if self.channel.recv_ready():
                    data = self.channel.recv(4096).decode("utf-8", errors="ignore")
                    if data:
                        self.output_received.emit(data)
                elif self.channel.exit_status_ready():
                    break
                time.sleep(0.1)

        except Exception as e:
            self.error_occurred.emit(str(e))
        finally:
            self.connection_closed.emit()

    def send_command(self, command: str):
        """发送命令到远程 shell"""
        if self.channel and self.running:
            self.channel.send(command)

    def close(self):
        """关闭连接"""
        self.running = False
        if self.channel:
            self.channel.close()
        if self.client:
            self.client.close()

配置说明(servers.json)

服务器配置保存在 servers.json 文件中,包含以下字段:

安全提示:

打包(PyInstaller)

推荐直接使用仓库内的 RemoteTerminalPython.spec

pyinstaller --noconfirm --clean RemoteTerminalPython.spec

如果要用命令行参数方式(Windows 示例,注意 --add-data 分隔符为 ;):

pyinstaller --noconfirm --clean --windowed --onedir --name "RemoteTerminalPython" --add-data "servers.json;." RemoteTerminal_python.py

依赖库

以上就是Python基于PyQt5开发一个SSH远程登录终端工具的详细内容,更多关于Python SSH远程登录的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文