Python3 PyQt5 - QLineEdit | Validator | editingFisihed - Problem

Dear community,

i have a little problem in my python3 application,im sure that they are several ways of solutions.

i want my application to execute after geting the command: "!p!,
a ping scan, executed by os.system("ping -4 " + targetip). But therefor we need to get the targetip variable. i tried to do that by: self.terminal.append("ConsoleLog<>> Enter target-ip now: ")
self.cmd_inp.editingFinished.connect(self.pingx)
ef pingx(self):
sleep(3)
#self.cmd_inp.editingFinished.connect(self.pingx)
sleep(3)

    os.system("ping -4 " + self.cmd_inp.text())
    sleep(4)

– Here is my full code: The target code is tagged by #### #####

#!/usr/bin/env python3
# BlackzCmdl.py
# Author: BlackLeakz
# Version: 0.1a

import datetime
import os
import sys
import platform
import subprocess
import configparser
import time
from datetime import datetime
from os.path import *
from time import sleep
from PyQt5 import QtCore, QtGui, QtWidgets


timex = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("ConsoleLog<>> " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1026, 656)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 20, 951, 171))
        self.groupBox.setObjectName("groupBox")

        self.setEnvBtn = QtWidgets.QPushButton(self.groupBox)
        self.setEnvBtn.setGeometry(QtCore.QRect(180, 140, 80, 24))
        self.setEnvBtn.setObjectName("setEnvBtn")

        self.syspath_inp = QtWidgets.QLineEdit(self.groupBox)
        self.syspath_inp.setGeometry(QtCore.QRect(20, 50, 241, 24))
        self.syspath_inp.setObjectName("syspath_inp")

        self.syspathlabel = QtWidgets.QLabel(self.groupBox)
        self.syspathlabel.setGeometry(QtCore.QRect(20, 30, 111, 16))
        self.syspathlabel.setObjectName("syspathlabel")

        self.syspathlabel_2 = QtWidgets.QLabel(self.groupBox)
        self.syspathlabel_2.setGeometry(QtCore.QRect(20, 80, 111, 16))
        self.syspathlabel_2.setObjectName("syspathlabel_2")

        self.shellpath_inp = QtWidgets.QLineEdit(self.groupBox)
        self.shellpath_inp.setGeometry(QtCore.QRect(20, 100, 241, 24))
        self.shellpath_inp.setObjectName("shellpath_inp")

        self.syspathlabel_3 = QtWidgets.QLabel(self.groupBox)
        self.syspathlabel_3.setGeometry(QtCore.QRect(360, 30, 141, 16))
        self.syspathlabel_3.setObjectName("syspathlabel_3")

        self.configpath_inp = QtWidgets.QLineEdit(self.groupBox)
        self.configpath_inp.setGeometry(QtCore.QRect(360, 50, 241, 24))
        self.configpath_inp.setObjectName("configpath_inp")

        self.setConfBtn = QtWidgets.QPushButton(self.groupBox)
        self.setConfBtn.setGeometry(QtCore.QRect(610, 50, 80, 24))
        self.setConfBtn.setObjectName("setConfBtn")
        self.setConfBtn.clicked.connect(self.conf)

        self.syspathlabel_4 = QtWidgets.QLabel(self.groupBox)
        self.syspathlabel_4.setGeometry(QtCore.QRect(360, 90, 141, 16))
        self.syspathlabel_4.setObjectName("syspathlabel_4")

        self.exportPath_inp = QtWidgets.QLineEdit(self.groupBox)
        self.exportPath_inp.setGeometry(QtCore.QRect(360, 110, 241, 24))
        self.exportPath_inp.setObjectName("exportPath_inp")

        self.setExportpathBtn = QtWidgets.QPushButton(self.groupBox)
        self.setExportpathBtn.setGeometry(QtCore.QRect(610, 110, 80, 24))
        self.setExportpathBtn.setObjectName("setExportpathBtn")
        self.setExportpathBtn.clicked.connect(self.exportpath)

        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(20, 200, 951, 371))
        self.groupBox_2.setObjectName("groupBox_2")

        self.terminal = QtWidgets.QTextBrowser(self.groupBox_2)
        self.terminal.setGeometry(QtCore.QRect(0, 20, 951, 301))
        self.terminal.setObjectName("terminal")

        self.cmd_inp = QtWidgets.QLineEdit(self.groupBox_2)
        self.cmd_inp.setGeometry(QtCore.QRect(0, 330, 801, 24))
        self.cmd_inp.setObjectName("cmd_inp")
        self.cmd_inp.returnPressed.connect(self.send)

        self.sendBtn = QtWidgets.QPushButton(self.groupBox_2)
        self.sendBtn.setGeometry(QtCore.QRect(860, 330, 80, 24))
        self.sendBtn.setObjectName("sendBtn")
        self.sendBtn.clicked.connect(self.send)

        self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar.setGeometry(QtCore.QRect(20, 600, 951, 23))
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")

        self.groupBox_2.raise_()
        self.groupBox.raise_()
        self.progressBar.raise_()
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.terminal.append("ConsoleLog<>> Checking os... .. ." +  " || " + timex)
        osx = platform.system()

        if osx == "Windows":
            self.terminal.append("ConsoleLog<>> Detected os : " + osx +  " || " + timex)
            self.terminal.append("ConsoleLog<>> Supported os!" +  " || " + timex)


        if osx == "Linux":
            self.terminal.append("ConsoleLog<>> Detected os : " + osx +  " || " + timex)
            self.terminal.append("ConsoleLog<>> Supported os!" +  " || " + timex)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "BlackzCmdl || Version: 0.1a || (c) by BlackLeakz"))
        self.groupBox.setTitle(_translate("MainWindow", "Settings:"))
        self.setEnvBtn.setText(_translate("MainWindow", "Set"))
        self.syspathlabel.setText(_translate("MainWindow", "Enter System-Path:"))
        self.syspathlabel_2.setText(_translate("MainWindow", "Enter Shell-Path:"))
        self.syspathlabel_3.setText(_translate("MainWindow", "Enter config.ini-Path:"))
        self.setConfBtn.setText(_translate("MainWindow", "Set"))
        self.syspathlabel_4.setText(_translate("MainWindow", "Export new Path:"))
        self.setExportpathBtn.setText(_translate("MainWindow", "Set"))
        self.groupBox_2.setTitle(_translate("MainWindow", "Terminal:"))
        self.sendBtn.setText(_translate("MainWindow", "Send"))




    def conf(self):
        config = configparser.ConfigParser()
        dirx = os.getcwd()
        self.terminal.append("ConsoleLog<>> Current dir: " + dirx + " || " + timex)
        confpath = self.configpath_inp.text()
        confint = str(confpath)
        self.terminal.append("ConsoleLog<>> Set config-ini path to: " + confint + " || " + timex)


    def exportpath(self):
        exppath = self.exportPath_inp.text()
        self.terminal.append("ConsoleLog<>> Exporting PATH: " + exppath +  " || " + timex)


    def send(self):
        cmd = self.cmd_inp.text()
        self.terminal.append("ConsoleLog<>> Executing command: " + cmd +  " || " + timex)
        os.system(cmd)
        self.cmd_inp.clear()

        if cmd == "!cls":
            self.terminal.clear()
            self.terminal.append("ConsoleLog<>> Screen cleared. " +  " || " + timex)

        if cmd == "!x":
            self.terminal.append("ConsoleLog<>> Exiting. " +  " || " + timex)
            sys.exit()
####   This is the command function i use to initialize the ping function self.pingx() ####
        if cmd == "!p":
            self.terminal.append("ConsoleLog<>> Ping-function " +  " || " + timex)
            self.terminal.append("ConsoleLog<>> Running ping-test. " + " || " + timex)
            self.cmd_inp.clear()
            self.terminal.append("ConsoleLog<>> Enter target-ip now: ")
            self.cmd_inp.editingFinished.connect(self.pingx)
        #    self.cmd_inp.textChanged.connect(self.pingx)
            sleep(4)

#### ping test function #######
    def pingx(self):
        sleep(3)
        #self.cmd_inp.editingFinished.connect(self.pingx)
        sleep(3)
    
        os.system("ping -4 " + self.cmd_inp.text())
        sleep(4)



if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

In another case, i would like to init the ping-scan function by : “!p + targetip”


#!p + targetip#
!p -4 192.168.1.1
!p + targetip should execute the command os.system("ping -4 targetip")

a simple and quick reply would be nice as soon as possible.
Many thanks,
BlackLeakz

I would refactor all the log lines using a function to log a messages into terminal.

You do not seem to have defined target_ip anywhere. Where does it come from?

Useing sleep means the ui will freeze. If you want it to be responsive
you will need to run the slow operations in a background thread and
send results to the foreground thread.

I would not be parsing the cmd_inp in a GUI i would use buttons or menu items of some form to do cls and exit etc.

1 Like