Go Back   Tech Support Forum > Alternative Computing > Linux Support

linux/BASH scripting help

This is a discussion on linux/BASH scripting help within the Linux Support forums, part of the Tech Support Forum category. I am running a DD-WRT enabled Linksys WRT350N router To say I'm a novice bash/C/UNIX programmer is an overstatement, but


Reply
 
Thread Tools Search this Thread
Old 04-18-2012, 08:12 AM   #1
Registered Member
 
Join Date: Apr 2012
Posts: 1
OS: Win 7 ULTIMATE



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!

__________________
nitrocs is offline   Reply With Quote
Old 04-19-2012, 11:50 AM   #2
Registered Member
 
Legit's Avatar
 
Join Date: Apr 2012
Location: DC/Baltimore Metro Area
Posts: 48
OS: Lion, Vista & RHEL6



Cron only cares about full paths in the crontab, not in the script, that action works the same regardless.

Does the script run when you execute it locally but in Cron it just returns a blank body? Are you putting this in the root crontab or in user crontabs?

Sorry I don't have time right now to properly troubleshoot your script, just tossing out a few ideas.

__________________
Legit is offline   Reply With Quote
Old 04-19-2012, 12:19 PM   #3
Alternative Computing
Moderator
 
hal8000's Avatar
 
Join Date: Dec 2006
Location: United Kingdom
Posts: 2,087
OS: Linux Mint 16 Petra (KDE), Arch Linux, Ubuntu 13.10 Raspberry Pi Model B (running Raspmc)

My System


Quote:
Originally Posted by nitrocs View Post
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:

In typical fashion for discovering new horizons, I've met a new issue
When I run the script through the command line:


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:
Any assistance would be greatly appreciated!

I am surprised that the DD-WRT project allows you to run crontabs in the first place.
Good to know that not only flashing with linux firmware you have a headless system to work with.

Although your crontab works at the terminal you are calling your script from
/tmp/custom.sh

This is not where your crontab is executing from. Your crontab is
/opt/arpmail.sh

Without knowing what is in your arpmail.sh script, what I suggest you do is invoke
/opt/arpmail.sh when you have telnet (or ssh into your router).
You should then see output in your console that will give you a clue to whats going wrong.
__________________
hal8000 is online now   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2


All times are GMT -7. The time now is 09:01 AM.


Copyright 2001 - 2014, Tech Support Forum

Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts