Skip to content

Instantly share code, notes, and snippets.

@mattrasband
Last active December 30, 2015 23:29
Show Gist options
  • Save mattrasband/7900818 to your computer and use it in GitHub Desktop.
Save mattrasband/7900818 to your computer and use it in GitHub Desktop.
Check the current IP vs the previous known IP, mail out any changes uses the PyMailer script
#!/usr/bin/env bash
# == User Set ==
# py_mailer2 location
mailer='/root/server_scripts/py_mailer/py_mailer2.py'
# latest ip file location
latest_known_ip='/root/.ip'
#to
to='someone@example.com'
# Gmail Username / Password
username='dummy_email'
password='dummy_email_password'
# ====================================
# Options for sites to get public IP
options=( 'curl -s ifconfig.me' 'curl whatismyip.org' 'wget -qO- ipecho.net/plain' )
# IP Regex
REGEXip="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
# == FUNCTIONS ==
function check_for_assets {
if [ ! -e ${latest_known_ip} ]; then
# Filler IP, will evaluage to false.
echo "No IP file found, creating..."
echo '255.255.255.0' > ${latest_known_ip}
fi
if [ ! -e "${mailer}" ]; then
echo "No mailer script found, please verify and try again."
exit
fi
}
function get_current_ip {
echo "Obtaining current public IP address..."
for option in "${options[@]}"; do
current_ip=`${option}`
if [[ ${current_ip} =~ ${REGEXip} ]]; then
break
fi
done
}
function get_latest_known_ip {
echo "Obtaining the last known public IP..."
latest_ip=`cat ${latest_known_ip}`
}
function notify_if_diff {
echo "Comparing Current and Latest Known public IPs..."
if [[ ${current_ip} != ${latest_ip} ]]; then
echo "New IP found, mailing ${to} with the info."
echo "${current_ip}" > ${latest_known_ip}
python ${mailer} \
-t ${to} \
-s "Your IP has changed: ${current_ip}" \
-m "New home IP: ${current_ip}" \
-u "${username}" \
-p "${password}"
else
echo "Public IP is unchanged."
fi
echo "Done!"
}
function main {
check_for_assets
get_current_ip
get_latest_known_ip
notify_if_diff
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment