Last active
January 26, 2018 19:42
-
-
Save hwdsl2/42966071f794a4fa26be to your computer and use it in GitHub Desktop.
Auto IP Update Script for Amazon EC2 Classic AND VPC instances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Auto IP Update Script for Amazon EC2 Classic AND VPC instances | |
# | |
# 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/. | |
MAC_ADDR=$(cat /sys/class/net/eth0/address) | |
LOCAL_IP=($(curl -s "http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC_ADDR}/local-ipv4s/")) | |
PUBLIC_IP=($(curl -s "http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC_ADDR}/public-ipv4s/")) | |
SUBNET_CIDR=$(curl -s "http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC_ADDR}/subnet-ipv4-cidr-block") | |
LOCAL_IP1=${LOCAL_IP[0]}; LOCAL_IP2=${LOCAL_IP[1]} | |
PUBLIC_IP1=${PUBLIC_IP[0]}; PUBLIC_IP2=${PUBLIC_IP[1]} | |
IP1_COMP=$(curl -s "http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC_ADDR}/ipv4-associations/${PUBLIC_IP1}/") | |
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])$" | |
CIDR_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])(\/(3[0-2]|[1-2]?[0-9]))$" | |
[ ! -f /root/IPADDR ] && { echo "IPADDR file does not exist! Aborting."; exit 1; } | |
. /root/IPADDR | |
[[ ! "${OLD_LOCAL_IP1}" =~ ${IP_REGEX} ]] && { echo "OLD LOCAL IP NOT FOUND OR INVALID! Aborting."; exit 1; } | |
[[ ! "${OLD_PUBLIC_IP1}" =~ ${IP_REGEX} ]] && { echo "OLD PUBLIC IP NOT FOUND OR INVALID! Aborting."; exit 1; } | |
[[ ! "${LOCAL_IP1}" =~ ${IP_REGEX} ]] && { echo "LOCAL IP NOT FOUND OR INVALID! Aborting."; exit 1; } | |
[[ ! "${PUBLIC_IP1}" =~ ${IP_REGEX} ]] && { echo "PUBLIC IP NOT FOUND OR INVALID! Aborting."; exit 1; } | |
TWOIP=0; SWAPIP=0 | |
if [[ "${LOCAL_IP2}" =~ ${IP_REGEX} ]] && [[ "${PUBLIC_IP2}" =~ ${IP_REGEX} ]]; then | |
TWOIP=1 | |
if [ "${IP1_COMP}" == "${LOCAL_IP2}" ]; then | |
TEMPIP=${PUBLIC_IP1}; PUBLIC_IP1=${PUBLIC_IP2}; PUBLIC_IP2=${TEMPIP} | |
SWAPIP=1 | |
fi | |
fi | |
# 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_LOCAL_IP1" != "$LOCAL_IP1" ]] || [[ "$OLD_PUBLIC_IP1" != "$PUBLIC_IP1" ]]; then | |
sed -i "s/${OLD_LOCAL_IP1}/${LOCAL_IP1}/" YOUR_SOFTWARE_CONFIG_FILE | |
sed -i "s/${OLD_PUBLIC_IP1}/${PUBLIC_IP1}/" YOUR_SOFTWARE_CONFIG_FILE | |
sed -i "s/${OLD_LOCAL_IP1}/${LOCAL_IP1}/" YOUR_IPTABLES_RULES_FILE | |
iptables -D ... ${OLD_LOCAL_IP1} ... # FILL IN YOUR DETAILS HERE | |
iptables -A ... ${LOCAL_IP1} ... # FILL IN YOUR DETAILS HERE | |
# FOR PBX IN A FLASH (PIAF) USERS: | |
# sed -i "s/${OLD_LOCAL_IP1}/${LOCAL_IP1}/" $FL1 | |
# sed -i "s/${OLD_PUBLIC_IP1}/${PUBLIC_IP1}/" $FL1 | |
# FOR OPENSWAN/STRONGSWAN/LIBRESWAN VPN USERS: | |
# sed -i "s/${OLD_LOCAL_IP1}/${LOCAL_IP1}/" $FL3 | |
# sed -i "s/${OLD_PUBLIC_IP1}/${PUBLIC_IP1}/" $FL3 | |
# sed -i "s/${OLD_PUBLIC_IP1}/${PUBLIC_IP1}/" $FL4 | |
fi | |
# FOR PBX IN A FLASH (PIAF) USERS: | |
# echo "tcpbindaddr=${LOCAL_IP1}" > $FL2; echo "bindaddr=${LOCAL_IP1}" >> $FL2 | |
# echo "externip=${PUBLIC_IP1}" >> $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 | |
echo "OLD_PUBLIC_IP1=${PUBLIC_IP1}" > /root/IPADDR | |
echo "OLD_LOCAL_IP1=${LOCAL_IP1}" >> /root/IPADDR | |
if [[ "${OLD_SUBNET_CIDR}" =~ ${CIDR_REGEX} ]] && [[ "${SUBNET_CIDR}" =~ ${CIDR_REGEX} ]]; then | |
if [ "${OLD_SUBNET_CIDR}" != "${SUBNET_CIDR}" ]; then | |
sed -i "s|${OLD_SUBNET_CIDR}|${SUBNET_CIDR}|" YOUR_SOFTWARE_CONFIG_FILE | |
fi | |
echo "OLD_SUBNET_CIDR=${SUBNET_CIDR}" >> /root/IPADDR | |
fi | |
[ "$SWAPIP" == "1" ] && echo "IP_SWAPPED=1" >> /root/IPADDR | |
if [ "$TWOIP" == "1" ]; then | |
ip addr add dev eth0 "${LOCAL_IP2}/24" | |
echo "OLD_PUBLIC_IP2=${PUBLIC_IP2}" >> /root/IPADDR | |
echo "OLD_LOCAL_IP2=${LOCAL_IP2}" >> /root/IPADDR | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment