我有以下代码:
import xlsxwriter
word='Chain'
asterix='***'
with open('/media/sf_vboxshared/iptables-list.log', 'r') as file :
filedata = file.read()
filedata = filedata.replace('destination', 'destination xxx')
with open('/media/sf_vboxshared/iptables-list.log', 'w') as file:
file.write(filedata)
def create_chain(chain_segment):
chains=[]
chain_lines = [line for line in chain_segment.split('\n') if line]
for line in chain_lines:
chain={}
chain2={}
if word in line:
chain['type'] = line.split()[1]
chain2['type'] = line.split()[1]
s=line.split()[2]
chain2['num']= s[1:]
try:
chain2['pkts']=line.split()[4]
except IndexError:
pass
try:
chain2['bytes']=line.split()[6]
except IndexError:
pass
x=line.split()[3]
if ")" in x:
x=x[:-1]
chain2['pkts']=x
else:
chain2['target']=line.split()[3]
elif line[0].isdigit():
chain['num']=line[0]+line[1]+line[2]
chain['pkts']=line.split()[1]
chain['bytes']=line.split()[2]
chain['target']=line.split()[3]
chain['prot']=line.split()[4]
chain['opt']=line.split()[5]
chain['in']=line.split()[6]
chain['out']=line.split()[7]
chain['source']=line.split()[8]
chain['destination']=line.split()[9]
try:
chain['xxx']=line.split()[10]+" "+line.split()[11]
except IndexError:
pass
chains.append(chain2)
chains.append(chain)
chains=filter(None, chains)
chains=list(chains)
chained = [merge_dicts(chains[0], i) for i in chains[1:]]
return chained
def merge_dicts(x,y):
z=x.copy()
z.update(y)
return z
with open('/media/sf_vboxshared/iptables-list.log') as f:
log_content = f.read()
host_sections = [host for host in log_content.split('---') if host]
hosts = {}
for host in host_sections:
hostname, chains_segment = host.split('\n', 1)
hostname = hostname.strip()
chains=[]
for segment in chains_segment.split('\n\n'):
chains.extend(create_chain(segment))
hosts[hostname] = chains
workbook=xlsxwriter.Workbook('/media/sf_vboxshared/iptables.xlsx')
worksheet1=workbook.add_worksheet('Sheet1')
worksheet1.write(0,0,'hostname')
worksheet1.write(0,1,'chain')
worksheet1.write(0,2,'num')
worksheet1.write(0,3,'pkts')
worksheet1.write(0,4,'bytes')
worksheet1.write(0,5,'target')
worksheet1.write(0,6,'prot')
worksheet1.write(0,7,'opt')
worksheet1.write(0,8,'in')
worksheet1.write(0,9,'out')
worksheet1.write(0,10,'source')
worksheet1.write(0,11,'destination')
worksheet1.write(0,12,'inventory')
row = 1
for host, chains in hosts.items():
for chain in chains:
worksheet1.write(row, 1, chain.get('type'))
worksheet1.write(row, 0, host)
worksheet1.write(row, 2, chain.get('num'))
worksheet1.write(row, 3, chain.get('pkts'))
worksheet1.write(row, 4, chain.get('bytes'))
worksheet1.write(row, 5, chain.get('target'))
worksheet1.write(row, 6, chain.get('prot'))
worksheet1.write(row, 7, chain.get('opt'))
worksheet1.write(row, 8, chain.get('in'))
worksheet1.write(row, 9, chain.get('out'))
worksheet1.write(row, 10, chain.get('source'))
worksheet1.write(row, 11, chain.get('destination'))
worksheet1.write(row, 12, chain.get('xxx'))
row += 1
workbook.close()
代码输入:
---node1 ***host1
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 1 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:123
2 25 16K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:123
3 7 28 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
4 58 39K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
5 81K 25M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 398 packets, 23K bytes)
num pkts bytes target prot opt in out source destination
1 2K 3M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
---node2 ***host2
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:123
2 62 4K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:123
3 6 214 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
4 58 30K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 34 packets, 18K bytes)
num pkts bytes target prot opt in out source destination
1 27K 3M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
---node3 ***host3
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 32 4K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:993
2 10 24K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
3 36 59K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 57 packets, 3K bytes)
num pkts bytes target prot opt in out source destination
1 1K 3M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
---node4 ***host4
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 9K 8M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 1 78 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
4 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
5 52K 1M REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 1K packets, 1M bytes)
num pkts bytes target prot opt in out source destination
1 1K 3M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
---node5 ***host5
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 2M 2G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 21 13K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
5 2 50 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 72778 packets, 5392K bytes)
num pkts bytes target prot opt in out source destination
1 9M 3G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
---node6 ***host6
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 8K 8M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 1 93 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
3 1 60 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
4 44 20 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
5 59 29 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 8 packets, 42K bytes)
num pkts bytes target prot opt in out source destination
1 1M 7M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
---node7 ***host7
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 19K 21M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 26 10 ACCEPT tcp -- * * 0.0.0.0/0 10.243.0.43 tcp dpt:4000 ctstate NEW
8 60 45K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject
Chain OUTPUT (policy ACCEPT 11 packets, 60K bytes)
num pkts bytes target prot opt in out source destination
1 3K 41M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
目前,此代码具有以下输出:
我需要创建另一个完全像“host”的dict,但称为“inventory”。对于“库存”,代码应在文本文件中的“***”之后搜索,而不是在“---”之后搜索的“主机”。新更改后的输出应与下一个屏幕截图完全相同:
我们如何才能实现这一目标?
非常感谢,
罗曼
答案 0 :(得分:0)
这是执行此操作的代码(也许其他人会使用它):
for host, chains in hosts.items():
for chain in chains:
host1=host.split()[0]
inventory=host.split('***')[1]
worksheet1.write(row, 2, chain.get('type'))
worksheet1.write(row, 0, host1)
worksheet1.write(row, 1, inventory)
worksheet1.write(row, 3, chain.get('num'))
worksheet1.write(row, 4, chain.get('pkts'))
worksheet1.write(row, 5, chain.get('bytes'))
worksheet1.write(row, 6, chain.get('target'))
worksheet1.write(row, 7, chain.get('prot'))
worksheet1.write(row, 8, chain.get('opt'))
worksheet1.write(row, 9, chain.get('in'))
worksheet1.write(row, 10, chain.get('out'))
worksheet1.write(row, 11, chain.get('source'))
worksheet1.write(row, 12, chain.get('destination'))
worksheet1.write(row, 13, chain.get('xxx'))
row += 1
workbook.close()
致以最诚挚的问候,
罗曼