Sometimes we are on connections that have a dynamic ip. This script will add your current external ip to ~/.external-ip.
Each time the script is run, it will
dig an OpenDNS resolver to grab your external ip. If it is different from what is in
~/.external-ip it will echo the new ip. Otherwise it will return nothing.
#!/bin/sh # Check external IP for change # Ideal for use in a cron job # # Usage: sh check-ext-ip.sh # # Returns: Nothing if the IP is same, or the new IP address # First run always returns current address # # Requires dig: # Debian/Ubuntu: apt install dnsutils # Solus: eopkg install bind-utils # CentOS/Fedora: yum install bind-utils # # by Sina Cutie <firstname.lastname@example.org> # Released under CC0 # Where we will store the external IP EXT_IP="$HOME/.external-ip" # Check if dig is installed if [ "$(command -v dig)" = "" ]; then echo "This script requires 'dig' to run" # Load distribution release information . /etc/os-release # Check for supported release; set proper package manager and package name if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then MGR="apt" PKG="dnsutils" elif [ "$ID" = "fedora" ] || [ "$ID" = "centos" ]; then MGR="yum" PKG="bind-utils" elif [ "$ID" = "solus" ]; then MGR="eopkg" PKG="bind-utils" else echo "Please consult your package manager for the correct package" exit 1 fi # Will run if one of the above supported distributions was found echo "Installing $PKG ..." sudo "$MGR" install "$PKG" fi # We check our external IP directly from a DNS request GET_IP="$(dig +short myip.opendns.com @resolver1.opendns.com)" # Check if ~/.external-ip exists if [ -f "$EXT_IP" ]; then # If the external ip is the same as the current ip if [ "$(cat "$EXT_IP")" = "$GET_IP" ]; then exit 0 fi # If it doesn't exist or is not the same, grab and save the current IP else echo "$GET_IP" > "$EXT_IP" fi