|
#!/bin/bash |
|
|
|
##################################################################### |
|
# Variables |
|
##################################################################### |
|
|
|
IP=$1 |
|
MODE=$2 |
|
DBTYPE=$3 |
|
|
|
file_geoip_country="/usr/share/GeoIP/GeoLite2-Country.mmdb" |
|
file_geoip_city="/usr/share/GeoIP/GeoLite2-City.mmdb" |
|
file_geoip_asn="/usr/share/GeoIP/GeoLite2-ASN.mmdb" |
|
|
|
file_dbip_country="/usr/share/DBIP/DBIPLite-Country.mmdb" |
|
file_dbip_city="/usr/share/DBIP/DBIPLite-City.mmdb" |
|
file_dbip_asn="/usr/share/DBIP/DBIPLite-ASN.mmdb" |
|
|
|
|
|
file_country="" |
|
file_city="" |
|
file_asn="" |
|
|
|
##################################################################### |
|
# Functions |
|
##################################################################### |
|
|
|
function getasvalue () { |
|
|
|
if [ $1 != "null" ]; |
|
then |
|
echo "AS$1 $2" |
|
else |
|
echo "null" |
|
fi |
|
|
|
} |
|
|
|
function valid_ip() |
|
{ |
|
local ip=$IP |
|
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 |
|
} |
|
|
|
##################################################################### |
|
# Validate |
|
##################################################################### |
|
|
|
# No argument given? |
|
if [ -z "$IP" ]; then |
|
printf "\nUsage:\n\n geoip2lookup IP4_ADDRESS\n\n" |
|
exit 1 |
|
fi |
|
# Check ip validity. |
|
if ! valid_ip "$IP"; then |
|
printf "\nThis is not a valid IP address. Exiting.\n\n" |
|
exit 1 |
|
fi |
|
|
|
##################################################################### |
|
# Execute |
|
##################################################################### |
|
|
|
if [ -z "$DBTYPE" ]; |
|
then |
|
|
|
file_city=$file_geoip_city |
|
file_asn=$file_geoip_asn |
|
file_country=$file_geoip_country |
|
|
|
else |
|
|
|
case $DBTYPE in |
|
|
|
"geoip") |
|
file_city=$file_geoip_city |
|
file_asn=$file_geoip_asn |
|
file_country=$file_geoip_country |
|
;; |
|
|
|
"dbip") |
|
file_city=$file_dbip_city |
|
file_asn=$file_dbip_asn |
|
file_country=$file_dbip_country |
|
;; |
|
esac |
|
|
|
fi |
|
|
|
json=`mmdbinspect --db ${file_city} --db ${file_asn} "$IP" 2>/dev/null` |
|
|
|
json_city=$(echo $json | jq -r '.[0].Records') |
|
json_asn=$(echo $json | jq -r '.[1].Records') |
|
|
|
city=$(echo $json_city | jq -r '.[0].Record.city.names.en') |
|
subdivisions=$(echo $json_city | jq -r '.[0].Record.subdivisions[0].names.en') |
|
country=$(echo $json_city | jq -r '.[0].Record.country.names.en') |
|
continent=$(echo $json_city | jq -r '.[0].Record.continent.names.en') |
|
location_lat=$(echo $json_city | jq -r '.[0].Record.location.latitude') |
|
location_lon=$(echo $json_city | jq -r '.[0].Record.location.longitude') |
|
location_code=$(echo $json_city | jq -r '.[0].Record.location.metro_code') |
|
location_tz=$(echo $json_city | jq -r '.[0].Record.location.time_zone') |
|
postal=$(echo $json_city | jq -r '.[0].Record.postal.code') |
|
asn_number=$(echo $json_asn | jq -r '.[0].Record.autonomous_system_number') |
|
asn_name=$(echo $json_asn | jq -r '.[0].Record.autonomous_system_organization') |
|
|
|
|
|
function mode1 () { |
|
|
|
printf " |
|
City: ${city} |
|
Territory: ${subdivisions} |
|
Country: ${country} |
|
Continent: ${continent} |
|
Location (approx): ${location_lat},${location_lon} |
|
Metro Code: ${location_code} |
|
Timezone: ${location_tz} |
|
Postal Code: ${postal} |
|
ASN Number: ${asn_number} |
|
ASN Organization: ${asn_name} |
|
\n" |
|
|
|
} |
|
|
|
function mode2 () { |
|
printf "$IP, ${city}, ${country}, ${continent}, $(getasvalue "${asn_number}" "${asn_name}")\n" |
|
} |
|
|
|
function mode3 () { |
|
printf "${city}, ${country}, ${continent}, $(getasvalue "${asn_number}" "${asn_name}")\n" |
|
} |
|
|
|
if [ -z "$MODE" ]; |
|
then |
|
|
|
mode1 |
|
|
|
else |
|
|
|
case $MODE in |
|
|
|
"mode1") |
|
mode1 |
|
;; |
|
|
|
"mode2") |
|
mode2 |
|
;; |
|
|
|
"mode3") |
|
mode3 |
|
;; |
|
esac |
|
|
|
fi |