سؤال

how can I add grep output number line by line.

I have the below output file from

     pkts      bytes target     prot opt in     out     source               destination
       0        0 RETURN     0    --  *      *       0.0.0.0/0            192.168.1.117
       0        0 RETURN     0    --  *      *       192.168.0.1          0.0.0.0/0
       0        0 RETURN     0    --  *      *       0.0.0.0/0            192.168.0.1
  375993 19581223 RETURN     0    --  *      *       192.168.1.136        0.0.0.0/0
  752537 1043650417 RETURN     0    --  *      *       0.0.0.0/0            192.168.1.136
     123     9348 RETURN     0    --  *      *       192.168.1.100        0.0.0.0/0
     121     9196 RETURN     0    --  *      *       0.0.0.0/0            192.168.1.100

I want to add all bytes for each IP address and store it as variable for further calculation.

iptables -L RRDIPT -vnx -t filter | grep "192.168.1" | cut -d " " -f4

gives me the blanks and 19581223,1043650417,9348 and 9196 but I am struggling to add the output.

Can anyone guide me?

هل كانت مفيدة؟

المحلول

The following script tallies up the byte counts per destination IP. If you want per source IP then change the $9 to $8

iptables -L ... | awk '!/destination/{a[$9]+=$2}END{for(item in a)printf("%s\tTotal bytes: %s\n",item,a[item])}'

Input

pkts      bytes target     prot opt in     out     source               destination
   0        1 RETURN     0    --  *      *       0.0.0.0/0            192.168.1.117
   0        2 RETURN     0    --  *      *       192.168.0.1          0.0.0.0/0
   0        3 RETURN     0    --  *      *       0.0.0.0/0            192.168.0.1
   0        4 RETURN     0    --  *      *       0.0.0.0/0            192.168.1.117
   0        5 RETURN     0    --  *      *       192.168.0.1          0.0.0.0/0
   0        6 RETURN     0    --  *      *       0.0.0.0/0            192.168.1.117
   0        7 RETURN     0    --  *      *       192.168.0.1          0.0.0.0/0

Output

192.168.0.1     Total bytes: 3
0.0.0.0/0       Total bytes: 14
192.168.1.117   Total bytes: 11

نصائح أخرى

awk will make short work of that.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top