Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?
share|improve this question
up vote 2 down vote accepted

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}'
share|improve this answer
The version is 1.4.4. – nixnotwin Apr 4 '11 at 9:52
Hmmm... same here. iptables -vL should convert the bytes count. iptables -xvL, on the other hand, shows the full byte count. – pepoluan Apr 4 '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.… 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 4 '11 at 11:04
ahhh... if it's just CSV you need, I can help. see my edit :) – pepoluan Apr 4 '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 4 '11 at 16:49

AWK is your friend.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.