awk 数组输出

发布于 2024-10-14 08:55:28 字数 2404 浏览 2 评论 0原文

我有一个 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']

我想针对 ip_array 运行 iptables 输出并获取结果。 例如,

    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

从我之前的帖子中,我了解到我可以使用 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]}}'

,但由于 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

我想看到下面的输出

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

,我尝试使用

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;}}'

我声明的数组 < code>dl 作为全局数组,但我似乎无法访问这些值,例如 dl[0] 进行进一步处理。

有人可以帮忙吗?

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.
e.g

    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 e.g dl[0] for further processing.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宛菡 2024-10-21 08:55:28

尝试以下操作:

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]}}'

更改 startrangeendrange 值以适合您。

输出示例:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文