Skip to content

Instantly share code, notes, and snippets.

@petervanderdoes
Last active June 9, 2017 01:23
Show Gist options
  • Save petervanderdoes/5f6e81d8409645923f6c1cd57971d4bb to your computer and use it in GitHub Desktop.
Save petervanderdoes/5f6e81d8409645923f6c1cd57971d4bb to your computer and use it in GitHub Desktop.
Home IP Change

What is this

My home IP changes every now and then, I never had this problem with the many years I had Comcast but now with FIOS it happened three times in less than two months. Of course the IP change always happen in the middle of the night.

I have a server running outside my home network. If my home IP changes it can be problematic for several programs running on the server, mostly mail but also the ability to SSH to the server. I would have to log in to the server through the web interface, acknowledge the new IP is me, change the IP in several configuration files, and then restart the services.

I rather do this automatically :)

Requirements

  • Your home IP must be registered with a dynamic DNS service.
  • Your home cable modem must be able to update the IP with the DNS provider. I'm sure all of them are.

Installation

  • Put all four files in a separate directory.
  • Update the vars file with the correct information.
  • Run the script change-ip through cron as root.
#!/bin/sh
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
# Run script to check if the FIOS IP has changed
${SCRIPTPATH}/check-ip
# Script return exit code 127 if it detected a change
if [ "$?" -eq 127 ]; then
# Load the var
. ${SCRIPTPATH}/vars
# Replace the IP address in the necessary file
sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/shorewall/rules
sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/fail2ban/jail.local
sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/postfix/rbl_override
sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/postfix/postscreen_access.cidr
sed -i "s/${OLD_IP}/${NEW_IP}/g" /etc/spamassassin/local.cf
# Compile files and restart services
cd /etc/postfix
postmap rbl_override
systemctl restart fail2ban.service
systemctl restart shorewall.service
systemctl restart postfix.service
systemctl restart spamassassin.service
echo "FIOS IP Changed. See separate email for more info"
echo "Old IP: ${OLD_IP}\nNew IP:${NEW_IP}" | mail -s "FIOS IP Changed" ${EMAIL}
${SCRIPTPATH}/update-ip-vars
fi
#!/bin/sh
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
. ${SCRIPTPATH}/vars
CURRENT_IP=`dig +short ${DOMAIN}`
# If the DNS lookup fails the return is empty.
if [ -z ${CURRENT_IP} ]; then
echo "${DOMAIN} resulted in an empty IP"
exit 1
fi
# The current IP has changed, recreat the vars file.
if [ ${CURRENT_IP} != ${OLD_IP} ]; then
exit 127
fi
# Nothing changed, we're all good.
echo "FIOS IP unchanged"
exit 0
#!/bin/sh
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
. ${SCRIPTPATH}/vars
CURRENT_IP=`dig +short ${DOMAIN}`
# If the DNS lookup fails the return is empty.
if [ -z ${CURRENT_IP} ]; then
echo "${DOMAIN} resulted in an empty IP"
exit 1
fi
# The current IP has changed, recreat the vars file.
if [ ${CURRENT_IP} != ${OLD_IP} ]; then
echo "DOMAIN=\"${DOMAIN}\"" > ${SCRIPTPATH}/vars
echo "OLD_IP=\"${NEW_IP}\"" >> ${SCRIPTPATH}/vars
echo "NEW_IP=\"${CURRENT_IP}\"" >> ${SCRIPTPATH}/vars
echo "EMAIL=\"${EMAIL}\"" >> ${SCRIPTPATH}/vars
exit 127
fi
# Nothing changed, we're all good.
exit 0
EMAIL="myemail"
DOMAIN="myhomedomain"
OLD_IP="x.x.x.x"
NEW_IP="x.x.x.x"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment