Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 78 lines (67 sloc) 2.06 KB
#!/bin/bash
#
# https://askubuntu.com/questions/95910/command-for-determining-my-public-ip
[ $# -gt 0 ] && action="$1" || action="one"
commands=(
# Go with a DNS query @ OpenDNS if we have the `dig` utility.
# (NOTE: must be the first entry, see EOF case in dns)...
"(type -p dig >/dev/null && dig +short myip.opendns.com @resolver1.opendns.com)"
"curl -s ipinfo.io/ip"
"(curl -s ipecho.net/plain && echo)"
"curl -s icanhazip.com"
"curl -s ipv4.icanhazip.com"
"(curl -s ident.me && echo)"
"(curl -s v4.ident.me && echo)"
"(curl -s v6.ident.me && echo)"
"curl -s http://checkip.amazonaws.com"
"curl -s http://smart-ip.net/myip"
"(wget -qO- http://ipecho.net/plain && echo)"
)
# FIXME: find out how to close the connection.
function geekiest_method_queries_stun_server() {
curl -s http://olegh.ftp.sh/public-stun.txt |
grep -v '^#' | column -t -s: | shuf -n1 |
( read server port &&
echo "Querying STUN server '$server' on port $port" &&
echo -en '\x00\x01\x00\x08\xc0\x0c\xee\x42\x7c\x20\x25\xa3\x3f\x0f\xa1\x7f\xfd\x7f\x00\x00\x00\x03\x00\x04\x00\x00\x00\x00' |
nc -u -w 2 "$server" "$port" |
dd bs=1 count=4 skip=28 2>/dev/null |
hexdump -e '1/1 "%u."' |
sed 's/\.$/\n/' )
}
case "$action" in
all)
for cmd in "${commands[@]}"; do
ip="$( bash -c "${cmd}" )"
echo "$ip $cmd"
done
;;
dns)
bash -c "${commands[0]}"
;;
# Iterate until one of the commands return something.
# (commands are shuffled).
one)
ip=""
let max_iter=${#commands[@]}
while [ -z "$ip" ] && [ $max_iter -gt 0 ] ; do
cmd="$(shuf -n1 -e "${commands[@]}")"
ip="$( bash -c "${cmd}" )"
echo -n "$ip"
type -p dig >/dev/null &&
echo -n " $(dig +short -x "$ip")"
echo " $cmd"
let max_iter=$max_iter-1
done
;;
stun)
geekiest_method_queries_stun_server
;;
*)
echo "ERROR: Unknown action '$action' (may be one of: 'one', 'all', 'stun')"
echo
echo "USAGE: $0 [all|one|sun]"
echo
exit 1
esac
## EOF ##