-1

I have a PyQT5 app which is a DialogBox with an embedded GroupBox. In the GroupBox I have some LineEdits and ComboBoxes for the user to complete. After that they submit this information that gets sent to the class ssh_session. I use Paramiko to open a session with the server and run some openssl commands. That works perfectly and the app receives back the results from the openssl command and stores it in stdout. I have two issues first after the results is returned the app closes I need it to remain open for the user to make other requests. Second I would like to send the stdout results to a textBrowser within the GroupBox, but have little luck in moving that data into the textBrowser.

Code:

import sys
import logging

# DEBUG  [10]   --   Detailed information, typically of interest only when diagnosing problems.

# INFO [20]     --    Confirmation that things are working as expected.

# WARNING [30]  --  An indication that something unexpected happened, or indicative of some problem in the near future (
#                   e.g. ‘disk space low’). The software is still working as expected.

# ERROR [40]    --    Due to a more serious problem, the software has not been able to perform some function.

# debug [50] -- A serious error, indicating that the program itself may be unable to continue running.

logging.basicConfig(filename='debug.log', level=logging.DEBUG, encoding='utf-8', datefmt='%m/%d/%Y %I:%M:%S %p',
                    format='%(asctime)s:%(levelname)s:%(message)s')

import paramiko
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QComboBox, QHBoxLayout, QPushButton, QLabel, QDialogButtonBox, \
    QMessageBox,QTextBrowser
from PyQt5.uic import loadUi


############################
#### Test login is server:14.49.18.14, User: root, Password: C1sc0123, TargetServer: 14.49.18.251 and choose from combobox###
############################


class Login(QDialog):
    def __init__(self):
        super(Login, self).__init__()
        logging.debug(f' LOADING PYQT5 CERTVERIFIER.UI WEB PAGE {loadUi, self}')
        loadUi("CertVerifier.ui", self)

        buttons = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
        self.buttonBox.accepted.connect(self.ssh_session)
        self.buttonBox.rejected.connect(self.reject)
        self.password.setEchoMode(QtWidgets.QLineEdit.Password)

    def reject(self):
        print("Cancel")
        self.close()

    #####################################################################
    ### TEST PRINT TO SEE IF OUTPUT IS USER INPUT-- TO def ssh_session(self)###
    #####################################################################

    def ssh_session(self):
        #################################
        #####Set the SSH Session With Paramiko SSHClient########
        #################################

        ssh = paramiko.SSHClient()
        logging.debug(f'PARAMIKO VARIABLE SET TO ssh: {ssh}')

        # Load SSH host keys
        ssh.load_system_host_keys()
        logging.debug(f'SSH KEYS SET: {ssh.load_system_host_keys}')
        print(ssh.load_system_host_keys())
        #

        # Add SSH host key automatically if needed.
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        print("Any Missing SSH Keys: ", ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()))
        logging.debug(f'ANY MISSING SSH KEYS: {ssh.set_missing_host_key_policy},{(paramiko.AutoAddPolicy())}')
        print("connecting")
        logging.debug(f' CONNECTION ATTEMPT: ')
        print()
        print()
        server_ip = self.server_ip.text()
        user = self.user.text()
        password = self.password.text()
        target_server = self.target_server.text()
        print("USER INPUT-- This Server Destination: ", server_ip)
        print("USER INPUT-- User: ", user)
logging.debug(
            f'USER INPUT-- Password: {password}')  # Come back and add a filter to encrypt the password in debug logs##
        logging.debug(f'USER INPUT-- This Target Test Server: {target_server}')
        logging.debug(f'USER INPUT-- This Port: {tport}')
        logging.debug(f'USER INPUT-- This Cipher: {ciphers}')
        logging.debug(f'THE APP RECEIVED THIS USER LOGIN: , {server_ip}, {user}, {password}')
        print("You received this login from app: ", server_ip, user, password)
        # Run command to fetch certs.

        get_cert = f'echo "Q" | openssl s_client -connect {target_server}:{self.tport.currentText()} -cipher {self.ciphers.currentText()}'

        stdin, stdout, stderr = ssh.exec_command(get_cert)

        logging.debug(
            f'APP SENT TO SSH CHANNEL: echo "Q" | openssl s_client -connect {target_server}:{self.tport.currentText()} -cipher {self.ciphers.currentText()}')
        print()
        print('##################################')
        print('CUCM Certificate Chain Trust: ')
        print('##################################')

        out = stdout.readlines()
        for out in out:
            print(out.strip())

        logging.debug(f' {stdin}')
        logging.debug(f' {stdout}')
        logging.debug(f' {stderr}')
        stdin.flush()
        ssh.close()
        logging.debug(f'{ssh.close()}')



    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Certificate Verify Close', 'Are you sure you want to quit?',
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
            logging.debug(f'{event.accept()}')
            self.close(event)
        else:
            event.ignore(self.ssh_session)

###########Run The Login Window################
class MainWindow(QtWidgets.QMainWindow, Login):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle('Windows')
    window = Login()
    window.show()
    window.setWindowTitle("Certificate Verification Tool")
    app.exec_()
    sys.exit(app.exec_())

IMPORTED FROM QT:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'C:\Users\jajanson\PycharmProjects\CertVerifier\CertVerifier.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_cert(object):
    def setupUi(self, cert):
        cert.setObjectName("cert")
        cert.resize(1920, 1080)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(cert.sizePolicy().hasHeightForWidth())
        cert.setSizePolicy(sizePolicy)
        cert.setMinimumSize(QtCore.QSize(799, 600))
        cert.setMaximumSize(QtCore.QSize(1920, 1080))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(False)
        font.setItalic(True)
        font.setWeight(9)
        cert.setFont(font)
        cert.setFocusPolicy(QtCore.Qt.ClickFocus)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("C:\\Users\\jajanson\\PycharmProjects\\CertVerifier\\../../OneDrive - Cisco/Pictures/tac byte bg.JPG"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        cert.setWindowIcon(icon)
        cert.setAutoFillBackground(False)
        cert.setStyleSheet("QDialog {\n"
"background:#ffffff;\n"
"opacity: 0.5;\n"
"font: 75 italic 12pt \"Arial\";\n"
"color: #ffffff\n"
"}")
        cert.setSizeGripEnabled(True)
        self.groupBox = QtWidgets.QGroupBox(cert)
        self.groupBox.setGeometry(QtCore.QRect(0, 0, 800, 600))
        self.groupBox.setMinimumSize(QtCore.QSize(800, 600))
        self.groupBox.setMaximumSize(QtCore.QSize(1920, 1080))
        font = QtGui.QFont()
        font.setFamily("Arial")
        self.groupBox.setFont(font)
        self.groupBox.setMouseTracking(True)
        self.groupBox.setStyleSheet("QGroupBox{\n"
"    background: #ffffff\n"
"    background-image: url(\"C:/Users/jajanson/PycharmProjects/window/;liketac.jpg\");\n"
"    background-repeat: no-repeat;\n"
"   background-position: center;\n"
"}\n"
"")
        self.groupBox.setLocale(QtCore.QLocale(QtCore.QLocale.Ewondo, QtCore.QLocale.Cameroon))
        self.groupBox.setObjectName("groupBox")
        self.tport = QtWidgets.QComboBox(self.groupBox)
        self.tport.setGeometry(QtCore.QRect(170, 150, 131, 18))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.tport.setFont(font)
        self.tport.setStyleSheet("QComboBox{\n"
"   background-color: #D6EAF8;\n"
"   border: 2px #2471A3;\n"
"   color: #000000;\n"
"   font: 12pt \"Arial\";\n"
"   border-radius:10px;\n"
"}\n"
"\n"
"QComboBox:focus {\n"
"   border: 2px solid #1A5276;\n"
"}\n"
"")
        self.tport.setEditable(True)
        self.tport.setMaxVisibleItems(10)
        self.tport.setObjectName("tport")
        self.tport.addItem("")
        self.tport.addItem("")
        self.tport.addItem("")
        self.tport.addItem("")
        self.tport.addItem("")
        self.tport.addItem("")
        self.target_server = QtWidgets.QLineEdit(self.groupBox)
        self.target_server.setGeometry(QtCore.QRect(170, 120, 133, 20))
        self.target_server.setStyleSheet("QLineEdit{\n"
"  background-color: #D6EAF8;\n"
"   border: 2px #2471A3;\n"
"   color: #000000;\n"
"   font: 12pt \"Arial\";\n"
"   border-radius:10px;\n"
"}\n"
"\n"
"QLineEdit:focus {\n"
"   border: 2px solid #1A5276;\n"
"}")
        self.target_server.setObjectName("target_server")
        self.label_7 = QtWidgets.QLabel(self.groupBox)
        self.label_7.setGeometry(QtCore.QRect(20, 180, 59, 19))
        self.label_7.setStyleSheet("QLabel {\n"
"background:#000000;\n"
"opacity: 0.5;\n"
"font: 75 italic 12pt \"Arial\";\n"
"color: #ffffff\n"
"}\n"
"")
        self.label_7.setObjectName("label_7")
        self.label_8 = QtWidgets.QLabel(self.groupBox)
        self.label_8.setGeometry(QtCore.QRect(20, 120, 119, 19))
        self.label_8.setStyleSheet("QLabel {\n"
"background:#000000;\n"
"opacity: 0.5;\n"
"font: 75 italic 12pt \"Arial\";\n"
"color: #ffffff\n"
"}\n"
"")
        self.label_8.setObjectName("label_8")
        self.server_ip = QtWidgets.QLineEdit(self.groupBox)
        self.server_ip.setGeometry(QtCore.QRect(170, 30, 133, 20))
        self.server_ip.setStyleSheet("QLineEdit{\n"
"  background-color: #D6EAF8;\n"
"   border: 2px #2471A3;\n"
"   color: #000000;\n"
"   font: 12pt \"Arial\";\n"
"   border-radius:10px;\n"
"}\n"
"\n"
"QLineEdit:focus {\n"
"   border: 2px solid #1A5276;\n"
"}")
        self.server_ip.setObjectName("server_ip")
        self.label_9 = QtWidgets.QLabel(self.groupBox)
        self.label_9.setGeometry(QtCore.QRect(20, 60, 87, 19))
        self.label_9.setStyleSheet("QLabel {\n"
"background:#000000;\n"
"opacity: 0.5;\n"
"font: 75 italic 12pt \"Arial\";\n"
"color: #ffffff\n"
"}\n"
"")
        self.label_9.setObjectName("label_9")
        self.password = QtWidgets.QLineEdit(self.groupBox)
        self.password.setGeometry(QtCore.QRect(170, 90, 133, 20))
        self.password.setStyleSheet("QLineEdit{\n"
"  background-color: #D6EAF8;\n"
"   border: 2px #2471A3;\n"
"   color: #000000;\n"
"   font: 12pt \"Arial\";\n"
"   border-radius:10px;\n"
"}\n"
"\n"
"QLineEdit:focus {\n"
"   border: 2px solid #1A5276;\n"
"}")
        self.password.setObjectName("password")
        self.label_10 = QtWidgets.QLabel(self.groupBox)
        self.label_10.setGeometry(QtCore.QRect(20, 30, 82, 19))
        self.label_10.setStyleSheet("QLabel {\n"
"background:#000000;\n"
"opacity: 0.5;\n"
"font: 75 italic 12pt \"Arial\";\n"
"color: #ffffff\n"
"}\n"
"")
        self.label_10.setObjectName("label_10")
        self.label_11 = QtWidgets.QLabel(self.groupBox)
        self.label_11.setGeometry(QtCore.QRect(20, 150, 101, 19))
        self.label_11.setStyleSheet("QLabel {\n"
"background:#000000;\n"
"opacity: 0.5;\n"
"font: 75 italic 12pt \"Arial\";\n"
"color: #ffffff\n"
"}\n"
"")
        self.label_11.setObjectName("label_11")
        self.user = QtWidgets.QLineEdit(self.groupBox)
        self.user.setGeometry(QtCore.QRect(170, 60, 133, 20))
        self.user.setStyleSheet("QLineEdit{\n"
"  background-color: #D6EAF8;\n"
"   border: 2px #2471A3;\n"
"   color: #000000;\n"
"   font: 12pt \"Arial\";\n"
"   border-radius:10px;\n"
"}\n"
"\n"
"QLineEdit:focus {\n"
"   border: 2px solid #1A5276;\n"
"}")
        self.user.setObjectName("user")
        self.label_12 = QtWidgets.QLabel(self.groupBox)
        self.label_12.setGeometry(QtCore.QRect(20, 90, 128, 19))
        self.label_12.setStyleSheet("QLabel {\n"
"background:#000000;\n"
"opacity: 0.5;\n"
"font: 75 italic 12pt \"Arial\";\n"
"color: #ffffff\n"
"}\n"
"")
        self.label_12.setObjectName("label_12")
        self.ciphers = QtWidgets.QComboBox(self.groupBox)
        self.ciphers.setGeometry(QtCore.QRect(170, 180, 341, 22))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.ciphers.setFont(font)
        self.ciphers.setStyleSheet("QComboBox{\n"
"   background-color: #D6EAF8;\n"
"   border: 2px #2471A3;\n"
"   color: #000000;\n"
"   font: 12pt \"Arial\";\n"
"}\n"
"\n"
"QComboBox:focus {\n"
"   border: 2px solid #1A5276;\n"
"}\n"
"")
        self.ciphers.setObjectName("ciphers")
        self.ciphers.addItem("")
        self.ciphers.addItem("")
        self.verticalScrollBar = QtWidgets.QScrollBar(self.groupBox)
        self.verticalScrollBar.setGeometry(QtCore.QRect(1130, 10, 16, 581))
        self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
        self.verticalScrollBar.setObjectName("verticalScrollBar")
        self.buttonBox = QtWidgets.QDialogButtonBox(self.groupBox)
        self.buttonBox.setGeometry(QtCore.QRect(620, 550, 156, 23))
        self.buttonBox.setStyleSheet("QPushButton {\n"
"\n"
"    background-color: #D6EAF8;\n"
"   \n"
"}\n"
"\n"
"QPushButton:hover{\n"
"\n"
"    background-color:  #5263ff;\n"
"    border-radius:10px\n"
"}")
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setProperty("QDialogButtonBox.Ok", "")
        self.buttonBox.setProperty("QDialogButtonBox.Cancel", "")
        self.buttonBox.setObjectName("buttonBox")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(-4, 12, 1081, 791))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("C:\\Users\\jajanson\\PycharmProjects\\CertVerifier\\../../Videos/Art/Like TAC page.JPG"))
        self.label.setScaledContents(True)
        self.label.setObjectName("label")
        self.textBrowser = QtWidgets.QTextBrowser(self.groupBox)
        self.textBrowser.setGeometry(QtCore.QRect(35, 251, 711, 241))
        self.textBrowser.setStyleSheet("QTextBrowser {\n"
" background-color: #D6EAF8;\n"
"   border: 2px #2471A3;\n"
"   opacity: 1.9;\n"
"   color: #000000;\n"
"   font: 12pt \"Arial\";\n"
"   border-radius:10px\n"
"}")
        self.textBrowser.setObjectName("textBrowser")
        self.label.raise_()
        self.tport.raise_()
        self.target_server.raise_()
        self.label_7.raise_()
        self.label_8.raise_()
        self.server_ip.raise_()
        self.label_9.raise_()
        self.password.raise_()
        self.label_10.raise_()
        self.label_11.raise_()
        self.user.raise_()
        self.label_12.raise_()
        self.ciphers.raise_()
        self.verticalScrollBar.raise_()
        self.buttonBox.raise_()
        self.textBrowser.raise_()

        self.retranslateUi(cert)
        self.buttonBox.accepted.connect(cert.accept) # type: ignore
        self.buttonBox.rejected.connect(cert.reject) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(cert)
        cert.setTabOrder(self.server_ip, self.user)
        cert.setTabOrder(self.user, self.password)
        cert.setTabOrder(self.password, self.target_server)
        cert.setTabOrder(self.target_server, self.tport)
        cert.setTabOrder(self.tport, self.ciphers)

    def retranslateUi(self, cert):
        _translate = QtCore.QCoreApplication.translate
        cert.setWindowTitle(_translate("cert", "Certificate Verifying Tool"))
        cert.setToolTip(_translate("cert", "<html><head/><body><p>Test Your Client Hello Server Hello</p></body></html>"))
        self.groupBox.setTitle(_translate("cert", "Enter Verification Information"))
        self.tport.setItemText(0, _translate("cert", "8443"))
        self.tport.setItemText(1, _translate("cert", "6972"))
        self.tport.setItemText(2, _translate("cert", "5061"))
        self.tport.setItemText(3, _translate("cert", "5091"))
        self.tport.setItemText(4, _translate("cert", "7400"))
        self.tport.setItemText(5, _translate("cert", "443"))
        self.label_7.setText(_translate("cert", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Cipher:</span></p></body></html>"))
        self.label_8.setText(_translate("cert", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Target Server:</span></p></body></html>"))
        self.server_ip.setToolTip(_translate("cert", "<html><head/><body><p>Enter your server to test TLS Verify</p></body></html>"))
        self.label_9.setText(_translate("cert", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Root User:</span></p></body></html>"))
        self.label_10.setText(_translate("cert", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Server IP:</span></p></body></html>"))
        self.label_11.setText(_translate("cert", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Target Port:</span></p></body></html>"))
        self.label_12.setText(_translate("cert", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Root Password:</span></p></body></html>"))
        self.ciphers.setItemText(0, _translate("cert", "ECDHE-RSA-AES256-GCM-SHA384"))
        self.ciphers.setItemText(1, _translate("cert", "ECDHE-ECDSA-AES256-GCM-SHA384"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    cert = QtWidgets.QDialog()
    ui = Ui_cert()
    ui.setupUi(cert)
    cert.show()
    sys.exit(app.exec_())

Tried creating a def to send the stdout to and call the textbrowser.
  • Please seperate the different modules in your code. I tried to edit it but it isn't clear where the boundaries are – Alexander Dec 22 '22 at 02:00
  • 1
    Please create a valid [mre] (possibly within a single code block). Also, remember that you can only ask *one* question per post; I suggest you to focus on the first question, as the other already lots of answer [in this post](https://stackoverflow.com/a/51641943) (please carefully read **all the answers** and their important warnings). Finally, learn how to use [layout managers](https://doc.qt.io/qt-5/layout.html). – musicamante Dec 22 '22 at 06:54

1 Answers1

0

When I checked the PyQT5 documentation the QDialog widget I was using will close when the singal slot completes. Changed the app to a MainWindow now the app stays open.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '22 at 14:40