Register
It is currently Sun Nov 23, 2014 12:46 pm

BASH scripting help


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Apr 18, 2012 9:19 am   

Joined: Wed Apr 18, 2012 9:18 am
Posts: 1
I am running a DD-WRT enabled Linksys WRT350N router
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 when clients sign on/off my network:

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


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

cron command i'm using is:
Quote:
*/5 * * * * root /opt/arpmail.sh


Any assistance would be greatly appreciated!


Top
 Profile  
 PostPosted: Wed Apr 18, 2012 6:20 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
hi,

you wrote absolute paths for files (filenames and path that should be variables), but not for commands.
`arp' is not in cron's default PATH.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 4 guests


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 post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP