Skip to contentSkip to conversation

Packets Counting

DISCUSSION M. A. Imam 5 participants 5 messages Mar 16, 2005

lore
Share & tools
lore

M. A. Imam

author
#0 lore raw

Hi,

How can i count the number of packets on an interface evry 2 or 5 seconds. and i want to count only specific packets like only arriving packets from port 5001

Any ideas...

Muhammad

#1 lore raw

Maybe you should use tcpdump for testing purposes instead of using iptables.

Just my opinion.

Regards,

Edvin

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of M. A. Imam
Sent: Mittwoch, 16. März 2005 16:56
To: [email protected]
Subject: Packets Counting

Hi,

How can i count the number of packets on an interface evry 2 or 5 seconds. and
i want to count only specific packets like only arriving packets from port 5001

Any ideas...

Muhammad

#2 lore raw
In reply to M. A. Imam
Quoted 11 lines · M. A. Imam

Hi,

How can i count the number of packets on an interface evry 2 or 5 seconds. and i want to count only specific packets like only arriving packets from port 5001

Any ideas...

Muhammad

iptables -L -v will show packet counts and byte counts on rules so, using your specific example and assuming you meant tcp port 5001

First create a matching rule that has no target
# iptables -I INPUT 1 -p tcp --dport 5001

Then, every x seconds run
iptables -L -v -n
and parse out the counter of interest.

Hope that helps,
SCampbell

#3 lore raw
In reply to M. A. Imam

Hello

Got a little script that i use to create graphs
with RRD tool.

The main idea is :
create a rule that will match packet that you want to count. It can be ACCEPTED packets for bandwidth monitoring or DROP / REJECT to know how many packets have been blocked on the firewall.

example rules that will count inbound and outbound bytes and packets for a NATed host :
$IPTABLES -t filter -A FORWARD -i eth0 -o ppp0 -s 192.168.0.2 -j ACCEPT $IPTABLES -t filter -A FORWARD -i ppp0 -o eth0 -d 192.168.0.2 -j ACCEPT
...

change these rules to fit what you want to count (in your question, it would be --sport 5001).

Then, another script to extract the information : "iptbwcheck.sh"

# extract information from iptables, put in a file and zero the counter (-Z)
# -x stands for "show info in bytes not megs or gigs"
/sbin/iptables -x -nvL -Z FORWARD >tmp_forward
# put these in local vars
inputPckMax=`grep "^.*ppp0   eth0.*192\.168\.0\.2 .*$" tmp_forward |awk
'{print $1}'`
inputBytesMax=`grep "^.*ppp0   eth0.*192\.168\.0\.2 .*$" tmp_forward |awk
'{print $2}'`
outputPckMax=`grep "^.*eth0   ppp0.*192\.168\.0\.2 .*$" tmp_forward |awk
'{print $1}'`
outputBytesMax=`grep "^.*eth0   ppp0.*192\.168\.0\.2 .*$" tmp_forward |awk
'{print $2}'`
# del tmp file
rm -f tmp_forward
# call RRD
endDate=`date +%s`
/usr/local/rrdtool-1.0.49/bin/rrdtool update nat.rrd \
$endDate:\
$inputBytesMax:$outputBytesMax:$inputPckMax:$outputPckMax
# do whatever else you want to do with this information
# ex : log in a file, log into mysql, upload to another syslog server, ...

See some graphs created from this script :

- Bandwidth monitoring
http://69.156.160.170/mephisto_day_full.gif

- Blocked packets / 5 mins
http://69.156.160.170/mephisto_day_full_firewall.gif

HTH

Maxime Ducharme
Programmeur / Spécialiste en sécurité réseau

----- Original Message -----
From: "M. A. Imam" <[email protected]>
To: <[email protected]>
Sent: Wednesday, March 16, 2005 10:56 AM
Subject: Packets Counting

Quoted 3 lines

Hi,

How can i count the number of packets on an interface evry 2 or 5 seconds.

and

Quoted 7 lines

i want to count only specific packets like only arriving packets from port 5001

Any ideas...

Muhammad

#4 lore raw
In reply to M. A. Imam

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 8 lines · M. A. Imam

Hi,

How can i count the number of packets on an interface evry 2 or 5 seconds. and i want to count only specific packets like only arriving packets from port 5001

Any ideas...

Or; how to chew up resources;

cron this to run every 3-5 seconds, minutes, hours, days or years are the desire hits you;

iptables -L -v |grep 5001

add redirection to a file and you'll have something more to play with.

Thanks,

Ron DuFresne
- -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
admin & senior security consultant: sysinfo.com
http://sysinfo.com

...Love is the ultimate outlaw. It just won't adhere to rules. The most any of us can do is sign on as it's accomplice. Instead of vowing to honor and obey, maybe we should swear to aid and abet. That would mean that security is out of the question. The words "make" and "stay" become inappropriate. My love for you has no strings attached. I love you for free...
-Tom Robins <Still Life With Woodpecker>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCOKZEst+vzJSwZikRAs3RAKCGI8Ji1bMGHHHebbAYi951EUFQQACgosUJ pSAQIRep4SsVhm82CxpoXMQ=
=XwHT
-----END PGP SIGNATURE-----