Find the answer to your Linux question:
Results 1 to 8 of 8
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    3

    BASH scripting help


    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:

    #!/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:

    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:
    */5 * * * * root /opt/arpmail.sh
    Any assistance would be greatly appreciated!

  2. #2
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    Quote Originally Posted by nitrocs View Post

    cron command i'm using is:
    Code:
    */5 * * * * root /opt/arpmail.sh
    What is the purpose of "root" here? You should have something more like:
    Code:
    */5 * * * * /bin/sh /opt/arpmail.sh
    Also, try redirecting the output of cron to see what the problem is:
    Code:
    */5 * * * * /bin/sh /opt/arpmail.sh > /tmp/arpmail.out 2>&1
    Last, check the mail for the user this is running as, since that user should be receiving the mail (locally, unless you setup forwarding, etc) about cronjobs.

    Also, you may need to check the $PATH for binaries inside the script. e.g., it may need "/usr/bin/sendmail" rather than just "sendmail". You also shouldn't "cat" into "grep" (or most pipes ever), but so long as you're happy with how the script works, that doesn't really need addressing.

  3. #3
    Just Joined!
    Join Date
    Apr 2012
    Posts
    3
    The CRON job runs fine, it executes the /opt/arpmail.sh script every 5 minutes as it should
    My issue is that when the script execution is automated, I get emails with no text in the body as if /opt/arpmsg.txt is not being inserted
    When I run the script manually (sh -x /opt/arpmail.sh) everything works fine & the body is inserted into an email properly
    SAMPLE OUTPUT:

    Address HWtype HWaddress Flags Mask Iface
    ool-4575xxx.dyn.optonl ether 00:01:5C:XX:XX:XX C vlan2
    titan ether 00:25:9C:XX:XX:XX C br0
    192.168.1.150 * * MP br0
    For some reason the CRON execution is not triggering the text insertion into the email body im not sure if i have all my commands correct, or even if I am missing something that CRON requires...help!

  4. #4
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    So what does the output e-mail from cron (not from this script, but from cron) say?

    If/when you redirect the output of the cron job to a file, what does it say?

  5. #5
    Just Joined!
    Join Date
    Apr 2012
    Posts
    3
    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:
    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)

  6. #6
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    Quote Originally Posted by nitrocs View Post
    arpmail.JPG

    the email on top was send when CRON was active
    the bottom email is from a manual execute command
    You're still not understanding. cron sends e-mails about every job to the local user who is running that job. What does that e-mail say? Not the e-mail from your script being manually run, not the e-mail from your script being run by cron, but the e-mail output from cron regarding the cron job.

    Also, the output from redirecting the cron output...

  7. #7
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    Quote Originally Posted by nitrocs View Post
    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:


    As a n00b, I dont know any other way to output the text from a file without loosing the formatting (i.e. echo)
    In most cases the error is simple ... the scipt runs 2-times the same time ...

    First I would try to use a lock so that the script won't run two times ...
    Usual "mkdir" is used to do so.

    Code:
    if mkdir /opt/cron.running; THEN
    ...
    rm -r /opt/cron.running
    fi
    Should do the job.
    Sry can't test at the moment.

    @dayid
    TO wrote he is running it on DD-WRT so chron won't send any mails itself

  8. #8
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    You may also should consider using the option:
    --dhcp-script=<path>
    Man page of DNSMASQ

    To trigger your script instead of cron.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •