因此,我有以下嵌套列表(已从Nmap XML输出文件中解析出)。这基本上是IP地址及其所有开放端口的列表:
[['192.168.23.78', ['53', '88', '135', '139', '389', '445', '3389']],
['192.168.27.243', ['135', '139', '445', '3389', '5800', '5900']],
['192.168.99.164', ['135', '139', '445', '3389', '5800', '5900']],
['192.168.228.211', ['80']],
['192.168.171.74', ['135', '139', '445', '3389', '5800', '5900']]]
我想根据此数据创建一个表,其中每个第一项(所有IP地址)都打印为行。然后,我希望遍历第二个项目(每个IP地址所有端口的列表),并计算每个唯一的端口号。我希望这个新计数的唯一端口列表可以打印为表的列标题。然后,我的空表应该大致如下:
53 80 88 135 139 389 445 3389 5800 5900
192.168.23.78
192.168.27.243
192.168.99.164
192.168.228.211
192.168.171.74
然后我希望将X放在具有给定开放端口的每个IP地址的每个正确单元格中,如下所示:
53 80 88 135 139 389 445 3389 5800 5900
192.168.23.78 X X X X X X
192.168.27.243 X X X X X X
192.168.99.164 X X X X X X
192.168.228.211 X
192.168.171.74 X X X X X X
我该如何处理我的数据集?
我是一个新手,但是我很可能想出如何遍历所有端口号并获得唯一的端口列表。但是我绝对不知道如何在正确的单元格中将X绘制在桌子上
到目前为止,这是我的代码:
#!/usr/bin/env python
from pprint import pprint
import xml.etree.ElementTree as ET
def loopy(item):
for port in host.findall('ports/port'):
if port.get('protocol') == "tcp":
portid = port.get('portid')
for state in port.findall('state'):
if state.get('state') == "open":
if item == "address":
list_addr.append(addr)
return
elif item == "portid":
list_portid.append(portid)
root = ET.parse('scan5.xml').getroot()
result = []
for host in root.findall('host'):
list_portid = []
list_addr = []
address = host.find('address')
addr = address.get('addr')
loopy("address")
loopy("portid")
if list_addr:
result.append([list_addr[0], list_portid])
pprint(result)
我的嵌套列表现在位于result
中,但是我不知道如何从中创建表。
到目前为止,我的代码仅生成原始列表:
[['10.133.23.78', ['53', '88', '135', '139', '389', '445', '3389']],
['10.133.27.243', ['135', '139', '445', '3389', '5800', '5900']],
['10.133.99.164', ['135', '139', '445', '3389', '5800', '5900']],
['10.135.228.211', ['80']],
['10.133.171.74', ['135', '139', '445', '3389', '5800', '5900']]]
答案 0 :(得分:0)
您可以使用install并使用prettytable包来可视化漂亮的表
第一个pip install prettytable
然后输入代码
from prettytable import PrettyTable
data = [['192.168.23.78', ['53', '88', '135', '139', '389', '445', '3389']],
['192.168.27.243', ['135', '139', '445', '3389', '5800', '5900']],
['192.168.99.164', ['135', '139', '445', '3389', '5800', '5900']],
['192.168.228.211', ['80']],
['192.168.171.74', ['135', '139', '445', '3389', '5800', '5900']]]
ports = sorted(set([int(port) for _, open_ports in data for port in open_ports]))
my_table = PrettyTable()
header = ['ip']
header.extend(ports)
my_table.field_names = header
for ip_address, open_ports in data:
row = [ip_address]
row.extend('X' if str(port) in open_ports else '' for port in ports)
my_table.add_row(row)
print(my_table)
输出
+-----------------+----+----+----+-----+-----+-----+-----+------+------+------+
| ip | 53 | 80 | 88 | 135 | 139 | 389 | 445 | 3389 | 5800 | 5900 |
+-----------------+----+----+----+-----+-----+-----+-----+------+------+------+
| 192.168.23.78 | X | | X | X | X | X | X | X | | |
| 192.168.27.243 | | | | X | X | | X | X | X | X |
| 192.168.99.164 | | | | X | X | | X | X | X | X |
| 192.168.228.211 | | X | | | | | | | | |
| 192.168.171.74 | | | | X | X | | X | X | X | X |
+-----------------+----+----+----+-----+-----+-----+-----+------+------+------+
答案 1 :(得分:0)
您可以使用numpy
和pandas
import pandas as pd
import numpy as np
table = [['192.168.23.78', ['53', '88', '135', '139', '389', '445', '3389']],
['192.168.27.243', ['135', '139', '445', '3389', '5800', '5900']],
['192.168.99.164', ['135', '139', '445', '3389', '5800', '5900']],
['192.168.228.211', ['80']],
['192.168.171.74', ['135', '139', '445', '3389', '5800', '5900']]]
# Collect the ip_addresses
ip_addresses = [el[0] for el in table]
# Collect the column names. Temporarily convert to integers to sort them properly
columns = sorted(np.unique([c for el in table for c in el[1]]), key = lambda x: int(x))
# Initialize numpy matrix to hold strings
table_matrix = np.zeros((len(ip_addresses), len(columns)), str)
for row in table:
# Get the row index of the IP address
for i, ip in enumerate(ip_addresses):
if row[0] == ip:
rdx = i
# Check which columns have values in the row that corresponds to the IP address
for c in row[1]:
for j, col in enumerate(columns):
# Add an X if the row has that column
if c == col:
table_matrix[rdx, j] = 'X'
# Create DataFrame
df = pd.DataFrame(table_matrix, index = ip_addresses, columns = columns)
输出:
Out[24]:
53 80 88 135 139 389 445 3389 5800 5900
192.168.23.78 X X X X X X X
192.168.27.243 X X X X X X
192.168.99.164 X X X X X X
192.168.228.211 X
192.168.171.74 X X X X X X