Skip to content

Instantly share code, notes, and snippets.

@hwdsl2
Last active January 2, 2016 04:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwdsl2/2a0bfc3465ba737f3bae to your computer and use it in GitHub Desktop.
Save hwdsl2/2a0bfc3465ba737f3bae to your computer and use it in GitHub Desktop.
Auto IP Update Script for DigitalOcean or Other Servers
#!/bin/bash
#
# Auto IP Update Script for DigitalOcean or Other Servers
#
# For detailed instructions, please see:
# https://blog.ls20.com/bash-script-for-automatic-ip-updates-on-amazon-ec2-or-digitalocean/
#
# Copyright (C) 2013 Lin Song
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/.
PUBLIC_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
IP_REGEX="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
[ ! -f /root/IPADDR ] && { echo "IPADDR file does not exist! Aborting."; exit 1; }
. /root/IPADDR
[[ ! "${OLD_PUBLIC_IP}" =~ ${IP_REGEX} ]] && { echo "OLD PUBLIC IP NOT FOUND OR INVALID! Aborting."; exit 1; }
[[ ! "${PUBLIC_IP}" =~ ${IP_REGEX} ]] && { echo "PUBLIC IP NOT FOUND OR INVALID! Aborting."; exit 1; }
# FOR PBX IN A FLASH (PIAF) USERS:
# FL1="/etc/asterisk/sip_general_additional.conf"
# FL2="/etc/asterisk/sip_general_custom.conf"
# FOR OPENSWAN/STRONGSWAN/LIBRESWAN VPN USERS:
# FL3="/etc/ipsec.conf"; FL4="/etc/ipsec.secrets"
if [ "$OLD_PUBLIC_IP" != "$PUBLIC_IP" ]; then
sed -i "s/${OLD_PUBLIC_IP}/${PUBLIC_IP}/" YOUR_SOFTWARE_CONFIG_FILE
sed -i "s/${OLD_PUBLIC_IP}/${PUBLIC_IP}/" YOUR_IPTABLES_RULES_FILE
iptables -D ... ${OLD_PUBLIC_IP} ... # FILL IN THE DETAILS HERE
iptables -A ... ${PUBLIC_IP} ... # FILL IN THE DETAILS HERE
# FOR PBX IN A FLASH (PIAF) USERS:
# sed -i "s/${OLD_PUBLIC_IP}/${PUBLIC_IP}/" $FL1
# FOR OPENSWAN/STRONGSWAN/LIBRESWAN VPN USERS:
# sed -i "s/${OLD_PUBLIC_IP}/${PUBLIC_IP}/" $FL3
# sed -i "s/${OLD_PUBLIC_IP}/${PUBLIC_IP}/" $FL4
fi
echo "OLD_PUBLIC_IP=${PUBLIC_IP}" > /root/IPADDR
# FOR PBX IN A FLASH (PIAF) USERS:
# echo "tcpbindaddr=${PUBLIC_IP}" > $FL2; echo "bindaddr=${PUBLIC_IP}" >> $FL2
# echo "externip=${PUBLIC_IP}" >> $FL2
# /usr/local/sbin/amportal kill
# /usr/local/sbin/amportal start
# FOR OPENSWAN/STRONGSWAN/LIBRESWAN VPN USERS:
# /sbin/service ipsec restart
# /sbin/service xl2tpd restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment