OK. Here are the scripts I use:
Add iptables script:
Code:
#!/bin/sh
AWK="/tmp/harddisk/busybox/busybox awk"
# create two chains for in and outbound traffic of LAN
iptables -N traffic
# add rules for traffic to and from router
if [ "$1" = "" ]; then
ip=`ifconfig eth1 | $AWK '/inet / {print substr($2, 6, 18)}'`
else
ip=$1
fi
iptables -A traffic -i eth1 -d $ip -j RETURN
iptables -A traffic -o eth1 -s $ip -j RETURN
# add rules for both directions for each IP address
# You can also use a simple textfile with ipadresses and use:
# for ip in `cat /tmp/harddisk/ipadresses.txt`
for ip in `$AWK '/192/ {print $2}' /etc/ethers`
do
iptables -A traffic -i eth1 -d $ip -j RETURN
iptables -A traffic -o eth1 -s $ip -j RETURN
done
#append general rules to track all traffic from unknown LAN addresses
iptables -A traffic -i eth1 -s ! 10.0.0.1 -j RETURN
iptables -A traffic -o eth1 -j RETURN
# attach chains to FORWARD, INPUT and OUTPUT
iptables -I FORWARD -j traffic
iptables -I INPUT 2 -j traffic
iptables -I OUTPUT -j traffic
To calculate traffic:
Code:
#!/bin/sh
AWK="/tmp/harddisk/busybox/busybox awk"
iptables -L traffic -vnx | $AWK '/all/ {if ($7=="*") inn=$2; else print $8, strftime("%Y-%m-%d %H:%M", systime()), "out", $2, "in", inn;}' > /tmp/traffic.dat
This results in a file like this:
Code:
*WAN_IP* 2005-04-07 09:36 out 29823 in 17965
192.168.100.100 2005-04-07 09:36 out 0 in 0
192.168.100.101 2005-04-07 09:36 out 0 in 0
192.168.100.102 2005-04-07 09:36 out 0 in 0
192.168.100.103 2005-04-07 09:36 out 0 in 0
192.168.100.104 2005-04-07 09:36 out 79434 in 276350
192.168.100.105 2005-04-07 09:36 out 0 in 0
192.168.100.106 2005-04-07 09:36 out 0 in 0
192.168.100.107 2005-04-07 09:36 out 0 in 0
192.168.100.108 2005-04-07 09:36 out 0 in 0
0.0.0.0/0 2005-04-07 09:36 out 0 in 0
What I do with this file is upload it to another server using curl where it is put in a db. You might want to store it on the usb-disk in some fashion. A tip here is to look at the account_traffic.sh script in the Graphical IP Accounting-scripts. That is where I got my "inspiration".
To make this script run every hour I use this script:
Code:
#!/bin/sh
if [ ! -f /var/spool/cron/crontabs/admin ]
then
mkdir -p /var/spool/cron/crontabs/
fi
echo "0 */1 * * * /tmp/harddisk/account.sh" >> /var/spool/cron/crontabs/admin
crond -L /dev/null
Then finally to start everything on reboot I put these lines in my post-mount script (See http://oleg.wl500g.info for details on post-boot/-firewall/-mount scripts):
Code:
. /tmp/harddisk/add_iptables.sh
. /tmp/harddisk/start_cron.sh
And I put this in post-firewall:
Code:
if [ -f /tmp/harddisk/add_trackers.sh ] ; then
. /tmp/harddisk/add_iptables.sh $2
fi
This is how I do it anyway, but if you look into shell scripting, awk and iptables you can do what you want with it. I am a bit of a Linux-n00b too, but the tutorials you need are out there.
S.