需求
扫描设定网段内 有哪些ip 地址并显示出列表。
使用关键模块
-
nmap
安装 nmap
先下载nmap的安装包
https://nmap.org/dist/nmap-7.80-setup.exe
然后把安装的路径导入环境变量
安装 python-nmap
pip install nmap
上代码:
def ScanIP_do(self):
nm = nmap.PortScanner() # instantiate nmap.PortScanner object
nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389')
hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
slm = QStringListModel(); # 创建mode
self.qList.clear() #重置清空列表
for host, status in hosts_list:
self.qList.append('{0}:{1}'.format(host, status))
print('{0}:{1}'.format(host, status))
slm.setStringList(self.qList) # 将数据设置到model
self.listView.setModel(slm) ##绑定 listView 和 model
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec_())