ARP command output/email script help

Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware
Author Message
nitrocs
DD-WRT User


Joined: 21 Jul 2009
Posts: 59

PostPosted: Tue Apr 17, 2012 14:53    Post subject: ARP command output/email script help Reply with quote
Router: WRT350N w/ optware
SVN: 17990 std-nokaid-usb


To say I'm a novice bash/C/UNIX programmer is an overstatement, but I learn as I go

I've cobbled together a script that emails me a daily bandwith log & it works...so I'm attempting to write my first script to get more info out of the router. I'd like to get an email whenever ANY (wired/wireless) active client comes on/off the network. This is what I have figured out so far:

Quote:

#!/bin/sh
arp>/opt/arpnew.lst
if ["$(cat /opt/arpnew.lst)" = "$(cat /opt/arpold.lst)"] ;
then [];
elseif
sendmail -S"mail.optonline.net" -f"xxx@xxx.net" -F"DD-WRT" -d"optonline.net" -s"DD-WRT Hosts change" -m"$(cat /opt/arpnew.lst)" xxx@gmail.com ;
cp /opt/arpnew.lst /opt/arpold.lst
fi


Ideally I'd like to put this in as a custom script with a CRON job to run every minute or two minues, but I cant seem to get it working!
Currently it does output to the arpnew.lst file, but does not seem to do the comparing OR copying to arpold.lst

Any assistance or pointers would be greatly appreciated!
Sponsor
nitrocs
DD-WRT User


Joined: 21 Jul 2009
Posts: 59

PostPosted: Tue Apr 17, 2012 23:28    Post subject: Reply with quote
Version 2:

Quote:

#!/bin/sh
arp > /opt/arpnew.lst
cmp -s /opt/arpnew.lst /opt/arpold.lst > /dev/null
if [ $? -eq 1 ];then
sendmail -S"mail.optonline.net" -f"admin@xxx.net" -F"DD-WRT" -d"optonline.net" -s"DD-WRT Hosts change" -m"$(cat /opt/arpnew.lst)" xxx@gmail.com ;
cp /opt/arpnew.lst /opt/arpold.lst
rm /opt/arpnew.lst
else
rm /opt/arpnew.lst
fi


The script is doing the work, but the sendmail function is not working...any tips?
nitrocs
DD-WRT User


Joined: 21 Jul 2009
Posts: 59

PostPosted: Wed Apr 18, 2012 0:58    Post subject: Reply with quote
Version 3:

This uses the sendmail function from my WORKING bandwith script, but does not send mail...please assist?

Quote:

#!/bin/sh

fnc_mail() { 

subj="$1" 
msg="$2" 

my_mail_addr="admin@xxx.net"
my_mail_to="xxx@gmail.com"
my_smtp="mail.optonline.net"
my_username="xxx"
my_passwd="xxx"
comcast="-d optonline.net"

if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/opt/lastsentmail.log"; else logfile="$3"; fi 

echo "From: $(nvram get router_name)<$my_mail_addr>" > /opt/arpmsg.txt
echo "Subject: $subj" >> /opt/arpmsg.txt
echo "To: $my_mail_to" >> /opt/arpmsg.txt

echo -e $msg >> /opt/arpmsg.txt

x=0
while [ $x -le 3 ] ;do

## rnr ##
cat /opt/arpmsg.txt | sendmail -S $my_smtp -f $my_mail_addr $comcast > $logfile 2>&1 

if [ "$(cat $logfile | grep 221 | awk '{print $1}')" = "221" ]; then break;fi

echo -e "\nSend Count = $x" >> /opt/arpmsg.txt

cat $logfile | grep -v 250 | grep -v 334 | grep -v 235 | grep -v 354 | \
grep -v 220 | grep -v 221 >> /opt/arpmsg.txt


sleep 120s

x=`expr $x + 1 `

done



arp > /opt/arpnew.txt
cmp -s arpnew.txt arpold.txt > /dev/null
if [ $? -eq 1 ]; 

then
msg=$(cat /opt/arpnew.txt) 
fnc_mail "Router Active Clients" "$msg" "/opt/bwmail.log" 
mv /opt/arpnew.txt /opt/arpold.txt
rm /opt/arpnew.txt

else
rm /opt/arpnew.txt
fi


Last edited by nitrocs on Wed Apr 18, 2012 3:21; edited 2 times in total
UrbanVoyeur
DD-WRT User


Joined: 19 Jun 2010
Posts: 118
Location: Brooklyn, NY

PostPosted: Wed Apr 18, 2012 1:07    Post subject: Reply with quote
This may be a stupid question, but is sendmail installed and running on your router?
_________________
Linksys E900 dd-wrt v24-sp2 18946 mini
Linksys E3000 dd-wrt v24-sp2 15692 std-usb-nas
D-Link DIR-601 Gargoyle 1.5.3 ar71xx dir 600 a1 squashfs
nitrocs
DD-WRT User


Joined: 21 Jul 2009
Posts: 59

PostPosted: Wed Apr 18, 2012 1:16    Post subject: Reply with quote
I'm going to say yes?
This is the original copy of the working bandwith script:

Quote:

#!/bin/sh
#
# 03-20-2012 --- RNR
# Brokeout all the personal variables to make it easier to modify.
# Added $comcast variable to make it work with . . .
#
# 03-07-2012 --- RNR
# Added DNS lease information to the end of the report.
#
# 01-24-2012 --- RNR
# Modified for DD-WRT Rev > 17628.
#
# 08-18-2011 --- RNR
# Bandwith report by email, custom script for DD-WRT .
# From: Useful Scripts Wiki --- Email Bandwidth Usage Daily
#

# Functions/Subs
#
fnc_mail() {

subj="$1"
msg="$2"

my_mail_addr="rstuvw@comcast.net"
my_mail_to="mnopqr@gmail.com"
my_smtp="smtp.comcast.net:587"
my_username="rstuvw@comcast.net"
my_passwd="MyOhMyIhateThis"
comcast="-d comcast.net"

if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastsentmail.log"; else logfile="$3"; fi

echo "From: $(nvram get router_name)<$my_mail_addr>" > /tmp/bwmsg.txt
echo "Subject: $subj" >> /tmp/bwmsg.txt
echo "To: $my_mail_to" >> /tmp/bwmsg.txt

echo -e $msg >> /tmp/bwmsg.txt

cat /tmp/dnsmasq.leases | awk '{ print NR,"\t", $4, "\t", $2, "\t", $3 ; }' >> /tmp/bwmsg.txt

x=0
while [ $x -le 3 ] ;do

## rnr ##
cat /tmp/bwmsg.txt | sendmail -S $my_smtp -f $my_mail_addr $comcast -au"$my_username" -ap"$my_passwd" > $logfile 2>&1

if [ "$(cat $logfile | grep 221 | awk '{print $1}')" = "221" ]; then break;fi

echo -e "\nSend Count = $x" >> /tmp/bwmsg.txt

cat $logfile | grep -v 250 | grep -v 334 | grep -v 235 | grep -v 354 | \
grep -v 220 | grep -v 221 >> /tmp/bwmsg.txt


sleep 120s

x=`expr $x + 1 `

done

}

#
# Main
#
aff="aff" #keyword workaround
yday=$(date -D %s -d $(( $(date +%s) - 86400)) +%d)
ymon=$(date -D %s -d $(( $(date +%s) - 86400)) +tr$aff-%m-%Y)
ydat=$(date -D %s -d $(( $(date +%s) - 86400)) +"%h %d")

upm=$(echo "Report time, UpTime and Current Load Average:")
upt=$(uptime)

if [ $(date +%d) -eq 1 ]; then monmsg="Last Month"; else monmsg="Month to Date"; fi
msg=$(nvram get $ymon)

msg=$(echo "$msg" | awk '{print $'$yday', $NF}')

tmp=$(echo "Totals for Yesterday")
tmp0=$(echo "$msg" | sed 's/\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)]/ Incoming: \1 MB == Outgoing: \2 MB == /')
tmp1=$(echo "Totals for '"${monmsg}"' including data sent after 0000 today")
tmp2=$(echo "$msg" | sed 's/\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)]/ Incoming: \3 MB == Outgoing: \4 MB == /')

tmp3=$(echo " IP: $(nvram get lan_ipaddr) DD-WRT: $(cat /tmp/loginprompt | grep -i release )")

who=$(echo "\"$(nvram get wl0.1_ssid)\" is connected to \"$(nvram get wl0_ssid) - - $(wl -i eth1 assoclist | awk '{print $2}')\" at this time.")
msg=$(echo "\n$who\n\n$upm\n $upt\n\n$tmp\n $tmp0\n\n$tmp1\n $tmp2\n\n$tmp3\n")

fnc_mail "Router Bandwidth Report for $ydat" "$msg" "/tmp/bwmail.log"

#
# End BWSend.sh
#


It emails just fine at 12:05 every day
nitrocs
DD-WRT User


Joined: 21 Jul 2009
Posts: 59

PostPosted: Wed Apr 18, 2012 1:56    Post subject: ARP/active client list email Reply with quote
Got it working! This script will output ARP to a file & check it against a "cached" version. If it is different, it will be emailed to you...if not different, it just resets itself for the next check. I wanted this so that I could see ALL active clients, not just wireless.

Code is as follows:

Quote:

#!/bin/sh

fnc_mail() {

subj="$1"
msg="$2"

my_mail_addr="ADMIN EMAIL ADDRESS"
my_mail_to="YOUR EMAIL ADDRESS"
my_smtp="mail.optonline.net"
my_username="xxx"
my_passwd="xxx"
comcast="-d optonline.net"

if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/opt/lastsentmail.log"; else logfile="$3"; fi

echo "From: $(nvram get router_name)<$my_mail_addr>" > /opt/arpmsg.txt
echo "Subject: $subj" >> /opt/arpmsg.txt
echo "To: $my_mail_to" >> /opt/arpmsg.txt

#echo -e $msg >> /opt/arpmsg.txt
cat /opt/arpnew.txt >> /opt/arpmsg.txt

x=0
while [ $x -le 3 ] ;do

## rnr ##
cat /opt/arpmsg.txt | sendmail -S $my_smtp -f $my_mail_addr $comcast > $logfile 2>&1

if [ "$(cat $logfile | grep 221 | awk '{print $1}')" = "221" ]; then break;fi

echo -e "\nSend Count = $x" >> /opt/arpmsg.txt

cat $logfile | grep -v 250 | grep -v 334 | grep -v 235 | grep -v 354 | \
grep -v 220 | grep -v 221 >> /opt/arpmsg.txt


sleep 120s

x=`expr $x + 1 `

done

}

arp > /opt/arpnew.txt
cmp -s /opt/arpnew.txt /opt/arpold.txt > /dev/null
if [ $? -eq 1 ];

then
msg=$(cat /opt/arpnew.txt)
fnc_mail "Router Active Clients" "$msg" "/opt/arpmail.log"
cp /opt/arpnew.txt /opt/arpold.txt
rm /opt/arpnew.txt

else
rm /opt/arpnew.txt
fi



I know its gross & probably can be refined...but its my first script!
I have it stored in /opt & it runs every 10 mins with a CRON job
Any thoughts, LMK
nitrocs
DD-WRT User


Joined: 21 Jul 2009
Posts: 59

PostPosted: Wed Apr 18, 2012 14:09    Post subject: Reply with quote
Well...in typical fashion for discovering new horizons, I've met a new issue

When I run the script through the command line:

Quote:

sh -x /tmp/custom.sh


It works just fine, but when the cron job runs I get emails w/ blank body
I know cron requires "absolute" paths (???) and I though I specified that everywhere, but I cant figure out whats going on now

Please assist
nitrocs
DD-WRT User


Joined: 21 Jul 2009
Posts: 59

PostPosted: Wed Apr 18, 2012 17:15    Post subject: Reply with quote
http://www.linuxforums.org/forum/attachments/programming-scripting/4221d1334768696-bash-scripting-help-arpmail.jpg

the email on top was send when CRON was active
the bottom email is from a manual execute command

if i had to guess, i'd say theres something funky with this part of my sendmail function:
Quote:

cat /opt/arpnew.txt >> /opt/arpmsg.txt


As a n00b, I dont know any other way to output the text from a file without loosing the formatting (i.e. echo)
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum