Graphical interface development based on PyQt5 – self-made ssh tool

Graphical interface development based on PyQt5 – self-made ssh tool

  • 0. Preface
  • 1. Installation of third-party libraries
  • 2. ssh principle
  • 3. Complete code
  • 4. Demonstration effect
  • 5. Other PyQt articles

0. Preface

In this section we use PyQt5 to make a simple ssh gadget.

OS: Windows 10 Professional

Development environment: Pycahrm Community 2022.3

Python interpreter version: Python3.8

Third-party libraries: PyQt5 and paramiko

1. Third-party library installation

This section requires the installation of third-party libraries PyQt5 and paramiko. If you are not familiar with the installation of third-party libraries, you can refer to the following articles to learn:
Python third-party library installation – use vscode, pycharm to install Python third-party libraries

2. ssh principle

SSH (Secure Shell) is an encrypted network protocol used to securely transmit data in an insecure network. It provides a secure remote login method to remotely access and control computers in an unsecured network.

The SSH protocol works as follows:

  1. The client initiates a connection request to the server, requesting to connect to the server’s SSH port (22 by default).

  2. After the server receives the connection request, it will send a public key to the client, and the client uses the public key to encrypt the data.

  3. The client sends the encrypted data to the server, and the server uses its own private key to decrypt the data.

  4. The server sends the decrypted data to the application program for processing, and the application program returns the processing result to the server.

  5. The server encrypts the processing result and sends it to the client, and the client uses its own private key to decrypt the data.

  6. The client displays the decrypted data to the user.

In the SSH protocol, an encryption algorithm is used during data transmission to ensure data security. At the same time, the SSH protocol also supports identity verification, which can use passwords, public keys, etc. for identity verification, ensuring the security of the connection.

3. Complete code

This time it is not like using Qtdesigner to design before, so only this one file is needed to run.

import sys
import paramiko
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTextEdit, QFileDialog, QMessageBox

class SSHWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('SSH Tool')
        self. setGeometry(100, 100, 800, 600)

        # Create the main window
        main_widget = QWidget(self)
        main_layout = QVBoxLayout()
        main_widget. setLayout(main_layout)
        self.setCentralWidget(main_widget)

        # Create connection information input box
        connect_widget = QWidget(self)
        connect_layout = QVBoxLayout()
        connect_widget. setLayout(connect_layout)
        main_layout. addWidget(connect_widget)

        host_layout = QHBoxLayout()
        host_label = QLabel('Host:')
        self. host_edit = QLineEdit()
        host_layout.addWidget(host_label)
        host_layout.addWidget(self.host_edit)
        connect_layout.addLayout(host_layout)

        port_layout = QHBoxLayout()
        port_label = QLabel('Port:')
        self. port_edit = QLineEdit()
        self.port_edit.setText('22')
        port_layout.addWidget(port_label)
        port_layout.addWidget(self.port_edit)
        connect_layout. addLayout(port_layout)

        username_layout = QHBoxLayout()
        username_label = QLabel('Username:')
        self. username_edit = QLineEdit()
        username_layout.addWidget(username_label)
        username_layout.addWidget(self.username_edit)
        connect_layout.addLayout(username_layout)

        password_layout = QHBoxLayout()
        password_label = QLabel('Password:')
        self. password_edit = QLineEdit()
        self.password_edit.setEchoMode(QLineEdit.Password)
        password_layout.addWidget(password_label)
        password_layout.addWidget(self.password_edit)
        connect_layout.addLayout(password_layout)

        # create connect button
        connect_button = QPushButton('Connect', self)
        connect_button.clicked.connect(self.connect_ssh)
        connect_layout.addWidget(connect_button)

        # Create command input box
        command_widget = QWidget(self)
        command_layout = QVBoxLayout()
        command_widget. setLayout(command_layout)
        main_layout. addWidget(command_widget)

        command_label = QLabel('Command:')
        command_layout.addWidget(command_label)

        self.command_edit = QTextEdit()
        command_layout.addWidget(self.command_edit)

        # create execute button
        execute_button = QPushButton('Execute', self)
        execute_button.clicked.connect(self.execute_command)
        command_layout.addWidget(execute_button)

        # create output box
        output_widget = QWidget(self)
        output_layout = QVBoxLayout()
        output_widget. setLayout(output_layout)
        main_layout. addWidget(output_widget)

        output_label = QLabel('Output:')
        output_layout.addWidget(output_label)

        self. output_edit = QTextEdit()
        self. output_edit. setReadOnly(True)
        output_layout.addWidget(self.output_edit)

    def connect_ssh(self):
        host = self.host_edit.text()
        port = int(self. port_edit. text())
        username = self. username_edit. text()
        password = self. password_edit. text()

        try:
            self.ssh = paramiko.SSHClient()
            self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.ssh.connect(host, port, username, password)
            self.output_edit.append('Connected to {}:{} as {}'.format(host, port, username))
        except Exception as e:
            QMessageBox. warning(self, 'Error', str(e))

    def execute_command(self):
        command = self.command_edit.toPlainText()

        try:
            stdin, stdout, stderr = self.ssh.exec_command(command)
            output = stdout. read(). decode('utf-8')
            error = stderr. read(). decode('utf-8')
            if output:
                self. output_edit. append(output)
            if error:
                self. output_edit. append(error)
        except Exception as e:
            QMessageBox. warning(self, 'Error', str(e))

if __name__ == '__main__':
    app = QApplication(sys. argv)
    window = SSHWindow()
    window. show()
    sys. exit(app. exec_())

4. Demo effect

Since my server is a public IP, it is blocked, and now we can connect to our server after clicking Connect:

After the connection is successful, a prompt will be given:

Now I typed some basic Linux commands to test:

whoami - output the currently logged in user
mkdir folder -- create a folder named folder
ls - view all files in the current directory

It seems that this test is still very successful, done!

5. Other PyQt articles

If you are more interested in PyQt5, you can refer to the following articles to communicate and learn together:
Graphical interface development based on PyQt5 – self-made MQTT client software
Graphical interface development based on PyQt5 – self-made Redis graphical client
Graphical interface development based on PyQt5 – simulated hospital management system
Graphical interface development based on PyQt5 – PyQt example_calculator
Graphical interface development based on PyQt5 – PyQt example Develop Paper
Graphical interface development based on PyQt5 – Windows memory resource monitoring assistant [compiled exe tutorial attached]