我是Windows和Python(主要是macOS / Ruby)编程的新手。我正在编写一个程序来扫描IP地址,查找联网设备,登录它们,然后升级它们的固件。我编写了一个Python脚本来执行此操作,并使用Tkinter进行GUI。当我在python中运行脚本时,一切都按预期工作。但是,在我从PyInstaller创建.exe后运行脚本时遇到问题。
当我尝试“映射网络”并使用python-nmap将输出导出到XML时,我认为它会引发错误。我在一个线程上有一个scan_network()函数,我检查它是否正在运行,当它完成时,我得到一个弹出文件“XML Generated”。当我将IP输入到GUI时,它几乎立即告诉我生成了XML,而不是XML。
我假设有一个问题,nmap没有捆绑到exe,或者其他什么,我不确定。有没有办法可以添加检查日志的方法?
以下是“扫描网络”按钮调用的功能。如何向此添加日志?
import nmap
def scan_network(ips):
nm = nmap.PortScanner()
nm.scan(hosts=ips, arguments='-v -A')
with open('xml/pyscan.xml', 'w') as xml_file:
xml_file.write(nm.get_nmap_last_output())
答案 0 :(得分:0)
做了更多研究。
这有效!
import nmap
import traceback
from utils import xml_parser
def scan_network(ips):
try:
nm = nmap.PortScanner()
nm.scan(hosts=ips, arguments='-v -A')
with open('xml/pyscan.xml', 'w') as xml_file:
xml_file.write(nm.get_nmap_last_output())
xml_parser.parse_xml('xml/pyscan.xml')
except:
f = open('nmap.log', 'w')
e = traceback.format_exc()
f.write(str(e))
f.close()