Last active
June 29, 2019 13:38
-
-
Save KaiserKatze/5a4547d3ab9aa37e15962250a3194364 to your computer and use it in GitHub Desktop.
Get external IP of your machine
This file contains hidden or 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 | |
printerr() { echo "$@" 1>&2; } | |
get_host_ip() { | |
adapters=$(ip -4 a | awk '/inet/{print $2}' | cut -d'/' -f1) | |
for adapter in $adapters; | |
do | |
# exclude address of loopback interface | |
if [ "$adapter" == 127.0.0.1 ]; | |
then | |
continue | |
fi | |
part1=$(echo $adapter | cut -d'.' -f1) | |
part2=$(echo $adapter | cut -d'.' -f2) | |
# exclude address reserved for private internets | |
case "$part1" in | |
# class A | |
10) | |
continue | |
;; | |
172) | |
case "$part2" in | |
# class b | |
16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31) | |
continue | |
;; | |
*) | |
echo "$adapter" | |
return 0 | |
;; | |
esac | |
;; | |
192) | |
case "$part2" in | |
# class c | |
168) | |
continue | |
;; | |
*) | |
echo "$adapter" | |
return 0 | |
;; | |
esac | |
;; | |
*) | |
echo "$adapter" | |
return 0 | |
;; | |
esac | |
done | |
printerr "External IP not found!" | |
return 1 | |
} | |
get_host_ip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment