简体   繁体   English

awk数组输出

[英]awk array output

i have an ip list array 我有一个IP列表数组

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. 我想针对ip_array运行iptables输出并获取结果。 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 从我以前的帖子中,我了解到可以使用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.. 但由于IP地址不断变化,我希望我的输出采用相同的格式。

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. 我将dl声明为全局数组,但似乎无法访问dl[0]等值进行进一步处理。

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. 更改开始startrangeendrange值以适合您。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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