juliushibert63
January 30th, 2012, 10:55 AM
I have a remote machine that occasionally changes IP locally and/or public IP to. I have DYNDNS setup as well to help combat this.
However I've written a small script that emails me my IP both local and public IPs setup as a cron job to run every 15 mins. The script works perfectly when run in a terminal window when I'm logged in either at the computer or via SSH. However as soon as the script is run as a cron job it only displays the public IP and not the local IP. It's almost as though ifconfig isn't working when run by cron.
Any ideas?
#!/bin/bash
# Email variables for Sendemail
#set -xv
# Email details
TO="*****"
FROM="*****"
SUBJECT="Current IP is "
MESSAGE_BODY=" "
# SMTP server details
SMTP_SERVER="******"
SMTP_USER="*****"
SMTP_PWD="*****"
SMTP_PORT="465"
send_email()
{
EMAIL_SUBJECT="${1}"
EMAIL_MESSAGE="${2}"
sendemail -f $FROM -t $TO -u "${EMAIL_SUBJECT}" -m "${EMAIL_MESSAGE}" -s $SMTP_SERVER -o tls=yes -xu $SMTP_USER -xp $SMTP_PWD
return 0
}
MESSAGE_BODY=`echo -e "\nExternal IP: "`
MESSAGE_BODY="${MESSAGE_BODY} `wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`"
MESSAGE_BODY="${MESSAGE_BODY} `echo -e "\n\nInternal IP: "`"
MESSAGE_BODY="${MESSAGE_BODY} `ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }'`"
MESSAGE_BODY="${MESSAGE_BODY} `echo -e "\n\n "`"
MESSAGE_BODY="${MESSAGE_BODY} `ifconfig`"
SUBJECT="${SUBJECT} (`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`)"
send_email "${SUBJECT}" "${MESSAGE_BODY}" # send email
exit 0
However I've written a small script that emails me my IP both local and public IPs setup as a cron job to run every 15 mins. The script works perfectly when run in a terminal window when I'm logged in either at the computer or via SSH. However as soon as the script is run as a cron job it only displays the public IP and not the local IP. It's almost as though ifconfig isn't working when run by cron.
Any ideas?
#!/bin/bash
# Email variables for Sendemail
#set -xv
# Email details
TO="*****"
FROM="*****"
SUBJECT="Current IP is "
MESSAGE_BODY=" "
# SMTP server details
SMTP_SERVER="******"
SMTP_USER="*****"
SMTP_PWD="*****"
SMTP_PORT="465"
send_email()
{
EMAIL_SUBJECT="${1}"
EMAIL_MESSAGE="${2}"
sendemail -f $FROM -t $TO -u "${EMAIL_SUBJECT}" -m "${EMAIL_MESSAGE}" -s $SMTP_SERVER -o tls=yes -xu $SMTP_USER -xp $SMTP_PWD
return 0
}
MESSAGE_BODY=`echo -e "\nExternal IP: "`
MESSAGE_BODY="${MESSAGE_BODY} `wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`"
MESSAGE_BODY="${MESSAGE_BODY} `echo -e "\n\nInternal IP: "`"
MESSAGE_BODY="${MESSAGE_BODY} `ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }'`"
MESSAGE_BODY="${MESSAGE_BODY} `echo -e "\n\n "`"
MESSAGE_BODY="${MESSAGE_BODY} `ifconfig`"
SUBJECT="${SUBJECT} (`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`)"
send_email "${SUBJECT}" "${MESSAGE_BODY}" # send email
exit 0