-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-my-ip.sh
executable file
·150 lines (124 loc) · 3.15 KB
/
get-my-ip.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
# This is a script of collection of all sorts of remote/outgoing IP address
# retrievals.
#
# Written by and maintained by Yu-Jie Lin
# This script is placed in public domain
#
# If you want to contribute to the list, please fork the Gist and commit, then
# send an email to me (check my GitHub profile for address) since comments on
# Gists generates no notifications. I will pull your Gist if I think I want
# them. Only you also agree to put your code in public domain.
#
# The services used are plain text in response, most are just IP, which is all
# we want. I will not add any services with HTML as response, even with good
# parsing.
#
# This script only works for IPv4 currently and it doesn't check if the
# returned text as IPv4 address.
#
# Blog : https://yjlv.blogspot.com/2014/02/getting-ip-address.html
# GitHub: https://github.com/lbgists/getmyip
RETR_CMD='curl -s -o -'
# Or if you prefer `wget`
# RETR_CMD='wget -q -O -'
############################################################
# functions in alphabetical order
gmip_admin_linux_fr() {
# http://www.commandlinefu.com/commands/view/12730/external-ip-raw-data
$RETR_CMD http://utils.admin-linux.fr/ip.php
}
gmip_curlmyip() {
# http://www.commandlinefu.com/commands/view/12742/external-ip-raw-data
$RETR_CMD http://curlmyip.com
}
gmip_externalip() {
# http://stackoverflow.com/a/9618532/242583
$RETR_CMD http://api.externalip.net/ip
}
gmip_icanhazip() {
$RETR_CMD http://icanhazip.com
}
gmip_ifconfig_me_ip() {
$RETR_CMD http://ifconfig.me/ip
}
gmip_ip_api() {
# http://ip-api.com/docs/api:newline_separated
$RETR_CMD http://ip-api.com/line | tail -1
}
gmip_ip_appspot() {
# also work with just HTTP
$RETR_CMD https://ipv4.appspot.com
}
gmip_ipinfo_io() {
# http://stackoverflow.com/a/19698908/242583
$RETR_CMD ipinfo.io/ip
}
gmip_myip_opendns() {
# http://www.commandlinefu.com/commands/view/4541/lookup-your-own-ipv4-address
dig +short @resolver1.opendns.com myip.opendns.com
}
gmip_telize() {
# http://www.telize.com/
$RETR_CMD http://www.telize.com/ip
}
gmip_trackip() {
# http://stackoverflow.com/a/19876824/242583
$RETR_CMD http://www.trackip.net/ip
}
############################################################
NS=gmip_
FUNCS=()
for f in $(compgen -A function); do
[[ $f != ${NS}* ]] && continue
FUNCS+=(${f/$NS/})
done
usage() {
echo "\
Usage: $(basename -- "$0") [OPTION] [NAMES]
Options;
-n dry run, don't actually access the services
-v verbose output, shows the function names with IPs
-h you are reading it
Names:
${FUNCS[@]}
" | fold -s
}
while getopts "nvh" opt; do
case $opt in
n)
DRYRUN=1
;;
v)
VERBOSE=1
;;
h)
usage
exit
;;
esac
done
shift $((OPTIND - 1))
if (($#)); then
FUNCS=($@)
fi
for f in ${FUNCS[@]}; do
if ! type $NS$f &>/dev/null; then
if [[ $VERBOSE ]]; then
printf "%-16s: %s\n" $f '*no such function*' >&2
else
echo "No such function: $f" >&2
fi
continue
fi
if [[ $DRYRUN ]]; then
IP='<IP>'
else
IP=$($NS$f)
fi
if [[ $VERBOSE ]]; then
printf "%-16s: %s\n" $f $IP
else
echo $IP
fi
done