This script automatically updates a local /var/log/ppp.log log from the latest router logs (otherwise the router logs are not timestamped and are deleted on router reboot).
Data from /var/log/ppp.log can be used track and audit broadband usage and to effectively compare with provider billing.
#!/bin/bash
# Retrieve router broadband usage logs.
# See /var/log/ppp.log for results.
# Run automatically ever hour by adding to /etc/cron.hourly as routerlog.
# Unfortunately as the router does not timestamp its entries - exactly duplicate entries will not be added to the log (how likely is this?)
# New entries to the log are sorted alphabetically (necessary for comm) rather than chronologically. It does not effect the overall audit.
user=admin
password=admin
log="/var/log/ppp.log"
search="es\." # happily identifies both lines for connection time and bytes sent and received
router="192.168.1.1"
sleeptime=4 # telnet pause
temp="/tmp/ppp.log"
# get latest router log
(sleep $sleeptime; echo "admin"; sleep $sleeptime; echo "admin"; sleep $sleeptime; echo "cat $log | grep '$search' "; sleep $sleeptime; echo "quit") | telnet $router | grep "$search" | tac | tr -d '\r\n' | sed 's/minutes\./\minutes\.\n/g' | tac | sed 's/Connect/ Connect/g' | sort > "$temp"
# compare overlapping lines in new log entries and complete log
touch "$log"
lines="$(wc -l $temp | cut -f 1 --delimit=" ")"
cat "$log" | grep "$search" | tail -$lines | sort -u > "$temp.0"
comm "$temp" "$temp.0" -2 -3 | cut -f 1- --delimit=" " > "$temp.1"
new="$(wc -l $temp.1 | cut -f 1 --delimit=" ")"
if [ $new -gt 0 ]
then
date +"(%Y/%m/%d %H:%M)" >> $log
cat "$temp.1" >> "$log"
fi
rm "$temp" "$temp.0" "$temp.1"
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment