-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyip
executable file
·54 lines (45 loc) · 1.24 KB
/
myip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh
# myip: get "external" (wan) ip address
dig="dig +short"
curl="curl --silent --fail --location --max-time 5"
# shuffle commands to not always "ping" the same server, but prefer dns queries
# (which are faster) over http apis
cmds="$(shuf << EOF
$dig @ns1-1.akamaitech.net ANY whoami.akamai.net
$dig @ns1.google.com TXT o-o.myaddr.l.google.com
$dig @resolver1.opendns.com myip.opendns.com
$dig @resolver2.opendns.com myip.opendns.com
$dig @resolver3.opendns.com myip.opendns.com
$dig @resolver4.opendns.com myip.opendns.com
EOF
)"
cmds="$cmds
$(shuf << EOF
$curl https://api.ipify.org
$curl https://checkip.amazonaws.com
$curl https://ident.me
$curl https://ifconfig.me/ip
$curl https://ipecho.net/plain
$curl https://ipinfo.io/ip
$curl https://myip.dnsomatic.com
EOF
)"
# set IFS to \n to iterate over lines
IFS='
'
for cmd in $cmds; do
ip="$(eval "$cmd")"
if [ "$?" -ne 0 ]; then
continue
fi
# the ip string returned by some service may be quoted, so tr removes
# (also) " chars
ip="$(printf '%s' "$ip" | tr -d '"[:space:]')"
if [ -z "$ip" ]; then
continue
fi
printf '%s\n' "$ip"
exit 0
done
echo "error: unable to get wan ip" >&2
exit 1