-
Notifications
You must be signed in to change notification settings - Fork 8
/
geolocate-ip
executable file
·129 lines (111 loc) · 2.88 KB
/
geolocate-ip
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
#!/bin/bash
usage(){
fmt <<EOF
DESCRIPTION
Use the ipinfodb.com API to geolocate a list of IP addresses from arguments, a file, or standard input. By default the API key for ipinfodb.com is read from ~/.config/geolocate-ip/ipinfodb.com-api-key
USAGE
geolocate-ip [-f,--file=FILENAME] [-c,--country-only] [-k,--api-key=APIKEY] [IPADDRESS[ IPADDRESS ...]]
-f,--file Read the IPs from the specified
file, one per line. Ignore any
IP addresses that are passed as
command-line arguments.
-c,--country-only Get country-level information
instead of city-level. Faster.
-k,--api-key Specify a ipinfodb.com API key
to use when making API requests.
EOF
exit
}
die(){ printf "Error: %s\n" "${1}" 1>&2; exit 1; }
require(){ command -v "${1}" > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then usage; fi fi
# End boilerplate
countryOnly=false
filename=""
configFilePath="$HOME/.config/geolocate-ip/ipinfodb.com-api-key"
apiKey=""
pause=false
ips=()
isValidIp(){
local ip="$1"
local stat=1
if [[ "${ip}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
geolocateIp(){
local ip=$1
if isValidIp $ip; then
if [ $countryOnly = true ]; then
loc=$(curl -s "http://api.ipinfodb.com/v3/ip-country/?key=$apiKey&ip=$ip" 2> /dev/null)
else
loc=$(curl -s "http://api.ipinfodb.com/v3/ip-city/?key=$apiKey&ip=$ip" 2> /dev/null)
fi
# Remove useless trailing information and prettyify the output a little
loc=$(echo "$loc" | sed 's/OK;;//')
loc=$(echo "$loc" | sed 's/;/\t/g')
echo "$loc"
fi
}
for i in "$@"
do
case $i in
-f=*|--file=*)
filename=$(echo "$i" | sed 's/[-a-zA-Z0-9]*=//')
;;
-c|--country-only)
countryOnly=true
;;
-k=*|--api-key=*)
apiKey=$(echo "$i" | sed 's/[-a-zA-Z0-9]*=//')
;;
esac
done
if [ "$apiKey" = "" ]; then
if [ -f "$configFilePath" ]; then
apiKey=$(cat "$configFilePath")
fi
fi
if [ "$apiKey" = "" ]; then
die "ipinfodb.com API key required."
fi
if [ "$filename" != "" ]; then
# We have input from a file
if [ -e "$filename" ]; then
for ip in $(cat "$filename")
do
ips=("${ips[@]}" "$ip")
done
fi
else
# We have piped input
if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
input=$(cat)
input=$(echo -e "$input" | sed 's/\s/\n/g')
while read -r ip; do
ips=("${ips[@]}" "$ip")
done <<< "$input"
fi
# We have input from arguments
for ip in $@
do
ips=("${ips[@]}" "${ip}")
done
fi
if [ ${# ips[@]} -gt 1 ]; then
pause="true"
fi
for ip in "${ips[@]}"
do
geolocateIp "${ip}"
if [ "${pause}" = "true" ]; then
sleep 1
fi
done