简体   繁体   中英

awk array output

i have an ip list array

ip_array=['192.168.1.100' '192.168.1.101' '192.168.1.102' '192.168.1.103' '192.168.1.104' '192.168.1.105' '192.168.1.106' '192.168.1.107' '192.168.1.108' '192.168.1.109' '192.168.1.110']

i want to run iptables output against the ip_array and get results. eg

    pkts      bytes target     prot opt in     out     source               destination
   83276  4337105   RETURN     0    --  *      *       192.168.1.106        0.0.0.0/0
  166008 230477883  RETURN     0    --  *      *       0.0.0.0/0            192.168.1.106
       0        0   RETURN     0    --  *      *       192.168.1.107        0.0.0.0/0
       0        0   RETURN     0    --  *      *       0.0.0.0/0            192.168.1.107
       0        0   RETURN     0    --  *      *       192.168.1.103        0.0.0.0/0
       0        0   RETURN     0    --  *      *       0.0.0.0/0            192.168.1.103
      99     9144   RETURN     0    --  *      *       192.168.1.102        0.0.0.0/0
      79    11590   RETURN     0    --  *      *       0.0.0.0/0            192.168.1.102
       0        0   RETURN     0    --  *      *       192.168.1.101        0.0.0.0/0
       0        0   RETURN     0    --  *      *       0.0.0.0/0            192.168.1.101
  994874 51992106   RETURN     0    --  *      *       192.168.1.100        0.0.0.0/0
 2398169 3594009427 RETURN     0    --  *      *       0.0.0.0/0            192.168.1.100
       0        0   RETURN     0    --  *      *       192.168.1.106        0.0.0.0/0
       0        0   RETURN     0    --  *      *       0.0.0.0/0            192.168.1.106

from my previous post I learnt that I can get the bytes info using awk

iptables -L RRDIPT -vnx -t filter |awk '!/destination/{a[$9]+=$2}END{for(item in a){total+=a[item];dl[item]=a[item];printf item"-"a[item]}}'

but since the ip address keep changing i want my output to be in the same format..

i.e bytesof 192.168.1.100, bytesof 192.168.1.102, bytesof 192.168.1.103, bytesof 192.168.1.104.......bytesof 192.168.1.110

i would like to see the below output

[3594009427,0,11590,0,0,0,230477883,0,0,0,0]

I tried using arrays

iptables -L RRDIPT -vnx -t filter |awk '!/destination/{a[$9]+=$2}END{for(item in a){if(item==ip_array[i]){dl[i]=a[item];printf dl[i];}else{dl[i]=0}i+=i;}}'

I declared dl as a global array but I cannot seem to access the values eg dl[0] for further processing.

Can anyone help?

Try this:

iptables ... | awk 'BEGIN { base="192.168.1"; startrange=100; endrange=110 } NR > 1 { a[$9] += $2} END {for (i=startrange; i<=endrange; i++) {ip = base "." i; if (! a[ip]) a[ip] = 0; print ip, a[ip]}}'

Change the startrange and endrange values to suit you.

Example output:

192.168.1.100 9196
192.168.1.101 0
192.168.1.102 0
192.168.1.103 0
192.168.1.104 21009126
192.168.1.105 0
192.168.1.106 0
192.168.1.107 10333
192.168.1.108 0
192.168.1.109 0
192.168.1.110 120

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