简体   繁体   中英

Shell keeps restarting while using a more complicated nmap command in subprocess in a PYQT5 GUI

I'm working on a Raspberry Pi 3, and at the moment, I'm trying to create a GUI in PyQt5 for some nmap commands.

More or less, I take input from a list, edit the subent, and from that, using various commands, it doesn't work. I am using subprocess for this to happen, due to the fact that I only require the output from the terminal.

Some commands from the nmap library work pretty well with no problems, and I manage to fetch the result, but with others the result is ongoing.

I try to print the result from this command:

sudo nmap -p 80,443 192.168.1.1/24 -oG -

And, the method that I'm using:

def nmapHttScan(self):
    subnet = str(self.leNmap.text())
    result = ' '
    res= subprocess.Popen(['sudo','nmap','-p 80,443',''+subnet,'-oG -'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,encoding = 'utf-8')
     while True:
        output=res.stdout.readline()
        if output =='' and res.poll() is not None:
            break;
        if output:
            print('output',output.strip())
        rc=res.poll()
     print(str(rc))

Every time I press the button in my GUI, so that I could see a result, my shell restarts and I don't really know what to do.

I think I may be using the process.poll() method wrong, but I've been working on this problem for about 4-5 days, and I have been searching everywhere. So, please, if you have any idea I'm willing to try anything.

Thank you, and have a nice day!

What you're doing seems to be unnecessarily complicated. Here it is a better way:

import sys
import os
from PyQt5.Qt import QApplication, QClipboard
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit, QPushButton
from PyQt5.QtCore import QSize

class ExampleWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(640, 440))    
        self.setWindowTitle("PyQt5 NMAP example") 

        # Add text field
        self.b = QPlainTextEdit(self)
        self.b.move(10,10)
        self.b.resize(600,300)

        # Create a button in the window
        self.button = QPushButton('Show text', self)
        self.button.move(120,380)

        # connect button to function on_click
        self.button.clicked.connect(self.functionnmap)

    def functionnmap(self):
        p = os.popen('sudo nmap -p 80,443 192.168.1.1/24 -oG -')
        output = p.read()
        self.b.insertPlainText(output)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = ExampleWindow()
    mainWin.show()
    sys.exit( app.exec_() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM