Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Created September 20, 2017 08:35
Show Gist options
  • Select an option

  • Save rawiriblundell/3c69a580abde3426f25af503736410ca to your computer and use it in GitHub Desktop.

Select an option

Save rawiriblundell/3c69a580abde3426f25af503736410ca to your computer and use it in GitHub Desktop.
Unused/untested function for converting hostnames to ip and the reverse
host2ip() {
# If $1 is missing, print brief usage
if [[ -n "$1" ]]; then
printf '%s\n' "Usage: host2ip [hostname|ip.add.re.ss]"
return 1
# If $1 appears to be a node name, figure out its IP
elif [[ "$1" =~ '^[a-zA-Z]$' ]]; then
host -4 -W 1 "$1" | awk '{print $4}'
# If $1 appears to be an IP address, try to figure the reverse
elif [[ "$1" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})[.]){3}(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})$' ]]; then
host -4 -W 1 "$1" | awk '{print $4}' | cut -d '.' -f1
else
printf '%s\n' "host2ip could not determine what is meant by: $1"
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment