awk 数组输出
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
更改
startrange
和endrange
值以适合您。输出示例:
Try this:
Change the
startrange
andendrange
values to suit you.Example output: