def convert_masscan_report(xml_path, xls_path): DOMTree = xml.dom.minidom.parse(xml_path) data = DOMTree.documentElement nodelist = data.getElementsByTagName('host') ip_info = {} for node in nodelist: scan_endtime = node.getAttribute('endtime') scan_endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(scan_endtime))) address_node = node.getElementsByTagName('address') addrtype = address_node[0].getAttribute('addrtype') addr = address_node[0].getAttribute('addr') port_node = node.getElementsByTagName('port') ip_prefix = addr.split(".")[0] + "." + addr.split(".")[1] + "." + addr.split(".")[2] if ip_prefix not in ip_info: ip_info[ip_prefix] = {} for port in port_node: if addr in ip_info[ip_prefix]: ip_info[ip_prefix][addr][1] = ip_info[ip_prefix][addr][1] + "," + portid continue protocol = port.getAttribute('protocol') portid = port.getAttribute('portid') state_element = port.getElementsByTagName('state') state = state_element[0].getAttribute('state') reason = state_element[0].getAttribute('reason') reason_ttl = state_element[0].getAttribute('reason_ttl') print('[+] | %s | %s | %s | %s | %s | %s | %s | %s |' % ( addr, portid, state, protocol, addrtype, reason, reason_ttl, scan_endtime)) scan_info = [addr, portid, state, protocol, addrtype, reason, reason_ttl, scan_endtime] ip_info[ip_prefix][addr] = scan_info workbook = xlsxwriter.Workbook(xls_path) for sheet_name, sheet_value in ip_info.items(): worksheet = workbook.add_worksheet(sheet_name) worksheet.autofilter("A1:H1") # 设置过滤 worksheet.freeze_panes(1, 0) # 冻结窗格 worksheet.lastrow = 0 summary_header = ["addr", "port", "state", "protocol", "addrtype", "reason", "reason_ttl", "scan_endtime"] for idx, item in enumerate(summary_header): worksheet.write(0, idx, item, workbook.add_format({"bold": True})) worksheet.lastrow += 1 for addr, addr_info in sheet_value.items(): for i in range(0, len(addr_info)): worksheet.write(worksheet.lastrow, i, addr_info[i]) worksheet.lastrow += 1 workbook.close()
时间: 2024-02-29 12:54:57 浏览: 23
这段代码的功能是将 Masscan 扫描结果的 XML 格式转换成 Excel 格式。具体实现的过程是:首先解析 XML 文件,然后遍历 XML 中的每个 host 节点,获取其中的 IP 地址,端口号等信息。将 IP 地址按照前三个段进行分组,最终将结果写入到 Excel 文件中,每个 IP 段对应一个 Excel Sheet,每个 Sheet 中包含了该 IP 段下的所有 IP 地址和对应的扫描结果。
相关问题
python如何使用xml.dom.minidom替换节点
使用 `xml.dom.minidom` 模块替换节点的步骤与使用 `xml.etree.ElementTree` 模块类似,但是代码略有不同。以下是一个简单的示例代码:
```python
import xml.dom.minidom
# 加载XML文件
dom = xml.dom.minidom.parse('example.xml')
root = dom.documentElement
# 查找要替换的节点
old_node = root.getElementsByTagName('old_node')[0]
# 创建新节点
new_node = dom.createElement('new_node')
new_node_text = dom.createTextNode('This is the new node.')
new_node.appendChild(new_node_text)
# 替换节点
root.replaceChild(new_node, old_node)
# 保存修改后的XML文件
with open('example.xml', 'w') as f:
dom.writexml(f)
```
在示例代码中,我们首先使用 `xml.dom.minidom.parse()` 方法加载XML文件,并获取XML根节点。然后使用 `getElementsByTagName()` 方法查找要替换的节点,使用 `createElement()` 方法创建新的节点,并使用 `createTextNode()` 方法创建新节点的文本内容。最后使用 `replaceChild()` 方法将旧节点替换为新节点,并使用 `writexml()` 方法保存修改后的XML文件。
需要注意的是,`xml.dom.minidom` 模块会将所有的空格、换行符等文本节点作为一个节点,因此在替换节点时需要特别注意。
xml.dom.minidom库如何删除、替换节点
要删除节点,可以使用 `removeChild(node)` 方法,其中 `node` 是要删除的节点对象。例如,如果要删除一个名为 `node1` 的节点,可以使用以下代码:
```python
xml_dom = xml.dom.minidom.parse("example.xml")
root = xml_dom.documentElement
node1 = root.getElementsByTagName("node1")[0]
root.removeChild(node1)
```
要替换节点,可以使用 `replaceChild(new_node, old_node)` 方法,其中 `new_node` 是替换后的节点对象,`old_node` 是要被替换的节点对象。例如,如果要将一个名为 `node1` 的节点替换为一个名为 `new_node1` 的节点,可以使用以下代码:
```python
xml_dom = xml.dom.minidom.parse("example.xml")
root = xml_dom.documentElement
node1 = root.getElementsByTagName("node1")[0]
new_node1 = xml_dom.createElement("new_node1")
root.replaceChild(new_node1, node1)
```