-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwimi
executable file
·106 lines (92 loc) · 1.93 KB
/
wimi
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
#!/bin/bash -
# Print "what is my IP address?" every 60 seconds (default) to standard output
AUTHOR="Bernard Lim"
PROG=$(basename $0)
urls=( \
"icanhazip.com" \
"my.ip.fi" \
"api.wipmania.com" \
"wtfismyip.com/text" \
"ipecho.net/plain" \
#"ifconfig.co/ip" \
"ifconfig.io/ip" \
"ifconfig.sexy/ip" \
"4.ifcfg.me/ip" \
)
# dependency message
depends() {
echo "$PROG: depends on \`$*'" >&2
echo "Install \`$*' and try again." >&2
exit 1
}
# die a graceful death!
die() {
echo "$PROG: $*" >&2
exit 1
}
# shorter usage
short() {
usage | sed '2q'
echo "Try \`$PROG -h' for more information."
}
# usage
usage() {
cat <<EOF
Usage: $PROG N
Print "what is my IP address?" for every N second(s). Default N is 60s.
EOF
}
goodbye() {
echo -e " Goodbye!" >&2
exit 0
}
trap goodbye INT
wip() {
if which curl &>/dev/null; then
while true; do
url=${urls[$RANDOM % ${#urls[@]}]}
ip=$(curl -m5 -s $url)
if [ $? -eq 0 ]; then
printf "%-20s" "${url%/*}" >&2
printf "%s\n" $(tr -cd '[0-9].' <<<"$ip")
break
fi
done
else
depends "curl"
fi
}
# parse short options
OPTIND=1
while getopts ":h" opt; do
case $opt in
h)
if [[ $# -ne 1 || $OPTIND -eq 1 ]]; then short; exit 1; fi
usage
exit 0
;;
\?) die "invalid option -- '$OPTARG'";;
esac
done
shift $((OPTIND-1))
# main
refresh=${1:-60}
if grep -Ev '^[0-9]+$' <<<"$refresh" >/dev/null; then
die "invalid refresh rate"
fi
if [ $refresh -eq 0 ]; then
while true; do
read -p "Warning: refresh rate is 0s. Do you want to continue [Y/n]? " yn
case $yn in
[Yy]*) break;;
[Nn]*) exit 1;;
*) echo -e "Please answer [Y]es or [n]o\n";;
esac
done
fi
printf "\nRefresh \`$PROG' every %ss. Ctrl-C to quit\n\n" "$refresh"
printf "%6s %-20s%-15s\n" "No." "Reference" "IP Address"
printf "%6s %-20s%-15s\n" "---" "---------" "----------"
while true; do
wip && sleep $refresh
done 2>&1 | cat -n