Skip to content

Instantly share code, notes, and snippets.

@kbabioch
Created April 22, 2017 20:07
Show Gist options
  • Save kbabioch/ca7a5bb578b4c1eec7078b00d1b53169 to your computer and use it in GitHub Desktop.
Save kbabioch/ca7a5bb578b4c1eec7078b00d1b53169 to your computer and use it in GitHub Desktop.
Checks whether the given hostname matches the own external IP address. The external IP address is determined by querying https://myip.babioch.de.
#! /bin/sh
# Copyright (c) 2017 Karol Babioch <[email protected]>
#
# 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/>.
# Verbosity flag
VERBOSE="False"
# Usage help
help()
{
cat << EOF
Copyright (c) 2017 Karol Babioch <[email protected]>
Checks whether the given hostname matches the own external IP address. The
external IP address is determined by querying https://myip.babioch.de.
Usage: $0 [OPTIONS]... HOSTNAME
OPTIONS:
-v, --verbose Enable verbose output
-h, --help Show this usage help
EOF
}
# Output a message and exit with given code
# $1: Exit code
# $2: Message (optional)
check_exit()
{
if [[ ! -z "$2" ]]; then
echo "$2"
fi
exit $1
}
# Output information in verbose mode
# $1: Message
verbose()
{
echo "$1" >&3
}
# Validate IP address with regular expression
# $1: IP
valid_ip()
{
if [[ "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
return 0
fi
return 1
}
# Parse options provided as command line arguments
for i in "$@"
do
case "$i" in
# Verbose mode
-v|--verbose)
VERBOSE="True"
shift
;;
# Usage help
-h|--help)
help
exit
;;
esac
done
# Check verbosity flag and attach new stream to stdout or /dev/null
if [[ "$VERBOSE" == "True" ]]; then
exec 3>&1
else
exec 3> /dev/null
fi
# Check number of arguments
if [[ "$#" != 1 ]]; then
check_exit 3 "Wrong number of arguments"
fi
HOSTNAME="$1"
IPA=$(curl -s https://myip.babioch.de)
IPB=$(dig +short "$HOSTNAME")
# Output debug information
verbose "Hostname: $HOSTNAME"
verbose "My IP: $IPA"
verbose "Hostname's IP: $IPB"
if ! valid_ip "$IPA"; then
check_exit 3 "Couldn't determine my IP address"
fi
if ! valid_ip "$IPB"; then
check_exit 3 "Couldn't determine IP of $HOSTNAME"
fi
if [[ "$IPA" == "$IPB" ]]; then
echo "My IP matches the one of $HOSTNAME: $IPB"
exit 0
fi
check_exit 2 "My IP ($IPA) differs from the one of $HOSTNAME ($IPB)"
@kbabioch
Copy link
Author

kbabioch commented Nov 9, 2017

Potential ToDos: Check if necessary tools are available, e.g. dig(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment