简体   繁体   中英

Python - New dict similar to another one added

I have the following code:

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()

Input for the code:

---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

Currently, this code has the following output:

当前

I would need to create another dict exactly like "host", but called "inventory". For "inventory", the code should search after "***" in the text file, not like "host" which is searching after "---". The output after the new change should be exactly like in the next screenshot:

新的输出

Any ideas how we can achieve this?

Many thanks in advance,

Romain

Here is the code that does this(maybe someone else will use it):

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()

Best regards,

Romain

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM