1

I use iptables based MAC access restriction. I have listed MAC id's of users whom I want to allow access, and drop rest. And when I run iptables -vL it shows all MAC id's and their usage: packets transferred, data in bytesh. So my questions are:

  1. Is it possible to make it show data usage in mega bytes?
  2. Can I sort it so that the MAC id's of heavy users will come at top?
  3. And, finally, can I save the data to disk, may be into a database, so that it can add up and survive reboots?
nixnotwin
  • 1,543
  • 5
  • 35
  • 55

2 Answers2

3

AWK is your friend. http://en.wikipedia.org/wiki/AWK

Porch
  • 680
  • 5
  • 12
2

Answer #1

What's your iptables --version?

On mine, iptables -vL automatically converts the bytes into Kbytes (K suffix) or Mbytes (M suffix).

Answer #2

Use iptables -x piped to sort, e.g. iptables -xvL $CHAIN | sort -rn -k 2

Answer #3

Pipe the output of the above commands to a script which will do the database insert.

You may want to further filter the output of Answer #2 using awk '$1 ~ /[0-9]+/' to remove the column headings, and awk '$1 > 1000000' to see only those values larger than 1'000'000 bytes.


Process output into CSV format

iptables -xvnL $CHAIN | awk -v min=$MINIMUM '$1 ~ /[0-9]+/ && $2 >= min {print $2 "," $11}'

Or, if you need the CSV column heading:

iptables -xvnL $CHAIN | awk -v min=$MINIMUM 'BEGIN {print "Bytes,MAC"} $1 ~ /[0-9]+/ && $2 >= min {print $2 "," $11}'
pepoluan
  • 5,038
  • 4
  • 47
  • 72
  • The version is `1.4.4`. – nixnotwin Apr 04 '11 at 09:52
  • Hmmm... same here. `iptables -vL` *should* convert the bytes count. `iptables -xvL`, on the other hand, shows the full byte count. – pepoluan Apr 04 '11 at 10:14
  • I also use `iptables -v -L -n`. On my desktop it shows in mega bytes. I will re-verify if it works on the ubuntu 10.04 based server at the place I work, and will let you know. Here is a solution to my third question which I found out after I asked the question here. http://www.linux.com/learn/tutorials/305767-bandwidth-monitoring-with-iptables But here it's very difficult to reuse the data, for e.g., if I want to generate a csv file to find out who consumes how much. – nixnotwin Apr 04 '11 at 11:04
  • ahhh... if it's just CSV you need, I can help. see my edit :) – pepoluan Apr 04 '11 at 12:14
  • Thank you very much. I will be using `iptables-save -c` to store the stats to disk and `iptables-restore` at boot up. And whenever required I will use your solution to export the data to csv file. – nixnotwin Apr 04 '11 at 16:49
  • @pepoluan I tried using csv export commands. Both the commands give ` /[0-9] run away regular expression error` – nixnotwin Apr 06 '11 at 00:45
  • @nixnotwin for some strange reason, the `/` following `+` got whacked. Fixed. Sorry for the error. – pepoluan Apr 06 '11 at 06:22