-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.sh
executable file
·147 lines (125 loc) · 4.05 KB
/
host.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
#!/bin/bash
# This script prints the current IP & its related info.
#
# If "country_asn.csv" is available, instead print the following:
# - CIDR
# - IP Range
# - Country
# - Continent
# - ASN
# - Company
# - Website
#
# Otherwise, print the following:
# - CIDR
# - Description
# - ASN
#
# The WHOIS query is sent to the Internet Routing Registry (RADdb)
# -----------------------------------------------------------------
# Written by Matteo Salonia (matteo@salonia.it)
# Check if we have a parameter
if [ $# -ne 1 ]; then
echo "Usage: $0 <IP>"
exit 1
fi
# Echo given IP
echo "IP: $1"
# Get this directory's path
DIRNAME=$(dirname "$0")
# country_asn.csv location
# You can get this file from the following website:
# https://ipinfo.io/products/free-ip-database
# (Choose IP to Country + ASN, CSV)
# The file is approximately 225 MB.
CSVFILE="$DIRNAME/country_asn.csv"
# Check if the file "country_asn.csv" exists
if [ ! -f "${CSVFILE}" ]; then
# Since we do not have the file, try to get
# as much info from the WHOIS lookup as possible.
# Check the following:
# - Route: CIDR
# - Descr: Description
# - Origin: ASN
# NOTE: multiple entries are allowed here
whois -h whois.radb.net $1 \
| grep -i "route\|descr\|origin" \
| sed 's/:[ ]\+/: /g'
exit
fi
# NOTE: before countinuing, make sure sipcalc is installed
which sipcalc >/dev/null 2>&1
if [ $? != 0 ]; then
echo "Please make sure 'sipcalc' is installed and available in PATH"
echo "https://github.com/sii/sipcalc"
exit
fi
# The country_asn.csv file exists.
# Perform a WHOIS lookup, retrieving only the first CIDR (route).
# NOTE: no multiple entries allowed (hence grep -m1)
cidr=$(
whois -h whois.radb.net $1 \
| grep -im1 "route" \
| sed 's/:[ ]\+/: /g' \
| awk '{print $2}'
)
# Check if CIDR isn't empty
if [ ! -z "${cidr}" ]; then
# Print CIDR
echo "CIDR: ${cidr}"
# Check if address is IPv4 or IPv6
grep "ipv4" <<<$(sipcalc "${cidr}") >/dev/null 2>&1
if [ $? != 0 ]; then
is_ipv4=0
else
is_ipv4=1
fi
if [ $is_ipv4 = 1 ]; then
# Get first IP address
start_ip=$(sipcalc "${cidr}" | awk '/Network address/ {print $4}')
# Replace start IP dots (.) with escaped dots (\.)
# so that the regex works properly
start_ip=$(sed "s/\./\\\./g" <<<$start_ip)
else
# Get network prefix
start_ip=$(sipcalc "${cidr}" | awk '/Compressed address/ {print $4}')
fi
# Find the line in the file
query=$(grep "^${start_ip}" "${CSVFILE}")
# Check query
if [ ! -z "${query}" ]; then
# Calculate range from CIDR instead of relying on DB data (IPv4 only)
if [ $is_ipv4 = 1 ]; then
range=$(sipcalc ${cidr} | awk '/Network range/ {print $4 " " $5 " " $6}')
else
range=$(cut -d',' -f1,2 <<<$query | sed "s/,/ - /")
fi
country=$(cut -d',' -f3 <<<$query)
country_name=$(cut -d',' -f4 <<<$query)
continent=$(cut -d',' -f5 <<<$query)
continent_name=$(cut -d',' -f6 <<<$query)
asn=$(cut -d',' -f7 <<<$query)
# Try to fetch company name
company=$(cut -d',' -f8 <<<$query)
# Check if the company name starts with a double quote (")
if [[ $company == \"* ]]; then
# If it does, we must instead fetch the full text
# between the double quotes, stripping them
# Example: "My Company" -> My Company
company=$(grep -oP '"[^"]+"' <<<$query | sed 's/"//g')
fi
website=$(awk -F, '{print $NF}' <<<$query)
#echo "Range: ${start_range} - ${end_range}"
echo "Range: ${range}"
echo "Country: ${country_name} (${country})"
echo "Continent: ${continent_name} (${continent})"
echo "ASN: ${asn}"
echo "Company: ${company}"
echo "Website: ${website}"
else
echo "Notice: unable to retrieve data for given CIDR."
fi
else
echo "Notice: unable to retrieve CIDR for IP '$1'"
echo "Either the DNS query failed, or the given IP is reserved for local use."
fi