?该demo,只能支持分割24位掩码的IP地址段。也未对输入内容做校验
逻辑代码部分:
import os
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from IPUI import *
class MyMainForm(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.execute)
def execute(self):
self.textBrowser.clear()
self.textBrowser.append("检测完成")
bianjieIP = []
newIPlist = []
IPaddressField = self.lineEdit.text()
bianjieIP = IPaddressField.split('-')
firstIP = bianjieIP[0]
newIPlist.append(firstIP)
firstIPlist = firstIP.split('.')
firstIPendNumber = firstIPlist[3] # 第一个IP的最后一位数
secondIP = bianjieIP[1]
secondIPlist = secondIP.split('.')
secondIPendNumber = secondIPlist[3] # 第二个IP的最后一位数
IpNum = int(secondIPendNumber)-int(firstIPendNumber) # IP 个数
# print(IpNum)
for a in range(0,IpNum):
IP = newIPlist[a] # 取第一个IP做为起始IP
IPendlist = IP.split('.') #分割IP
newIPend = str(int(IPendlist[3])+1) # 取IP最后一位进行加一
IPendlist[3] = newIPend # 替换最后一位IP数值,形成新的列表
newIP = '.'.join(IPendlist) # 新IP列表形成字符串
newIPlist.append(newIP) # 新IP插入IP列表
# print(newIPlist)
self.textBrowser.append(str(newIPlist))
for ip in range(0,len(newIPlist)):
ipaddr = newIPlist[ip]
backinfo = os.system('ping -n 2 -w 2 %s'%ipaddr)
if backinfo == 0:
# print("backinfo:"+str(backinfo))
self.textBrowser.append(ipaddr+"设备在线")
# print(ipaddr+"设备在线")
else:
self.textBrowser.append(ipaddr+"设备不在线")
# print(ipaddr+"设备不在线")
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())
UI代码部分:?
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'IPUI.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(448, 446)
MainWindow.setMinimumSize(QtCore.QSize(448, 446))
MainWindow.setMaximumSize(QtCore.QSize(448, 446))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(360, 20, 75, 21))
self.pushButton.setObjectName("pushButton")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(20, 50, 411, 331))
self.textBrowser.setObjectName("textBrowser")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 22, 54, 20))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(80, 20, 271, 20))
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 448, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "IP一键检测工具"))
self.pushButton.setText(_translate("MainWindow", "一键检测"))
self.label.setText(_translate("MainWindow", "IP地址段:"))
self.lineEdit.setText(_translate("MainWindow", "192.168.1.1-192.168.1.123"))
输出效果如图所示:
?
|