Skip to content

Instantly share code, notes, and snippets.

@danielscholl
Created November 16, 2017 22:05
Show Gist options
  • Save danielscholl/c6e39bcefecb8806fb1aaea7ce12bacf to your computer and use it in GitHub Desktop.
Save danielscholl/c6e39bcefecb8806fb1aaea7ce12bacf to your computer and use it in GitHub Desktop.
#!/bin/sh
# get internal IP address
# used for outgoing Internet connections
resolve() {
(gethostip -d $1 || getent ahostsv4 $t | grep RAW | awk '{print $1; exit}') 2>/dev/null
}
noip() {
[ -n "$(echo $1 | tr -d '0-9.\n')" ]
}
[ -n "$1" ] && t=$1 || t='8.8.8.8'
noip $t && t=$(resolve $t)
[ -n "$t" ] || { echo Cannot resolve domain $1 >&2; exit 1; }
ip route get $t | awk '{print $NF; exit}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment