python - 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_() )
推荐阅读
- javascript - Defining page direction dynamically - Angular 5
- multithreading - 使用不连续的内存访问评估最坏情况下的 RAM 有效带宽
- c# - 给对象一个带有变量的名字
- scala - Scala 列表转换为地图
- node.js - npm install 卡住 - extract:rxjs: 动词轻轻Rm 不关心内容;核弹
- moodle - 向自定义角色添加自定义功能
- sapui5 - 过滤条上的双点是什么?
- docusignapi - 启用/禁用编辑视图
- android - 检测奥利奥中的模拟位置
- python - Apache Beam python 从字典列表中获取不同的值