This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgeo
executable file
·127 lines (114 loc) · 3.11 KB
/
geo
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
#!/usr/bin/env bash
#
# Bash utility for getting specific network info
# Author: Jake Meyer
# Github: https://github.com/jakewmeyer
#
# Parse arguments passed + help formatting
usage() {
echo "Usage: geo [flag]"
echo " -w Returns WAN IP"
echo " -l Returns LAN IP(s)"
echo " -r Returns Router IP"
echo " -d Returns DNS Nameserver"
echo " -m Returns MAC address for interface. Ex. eth0"
echo " -g Returns Current IP Geodata"
echo "Custom Geo Output =>"
echo "[all] [query] [city] [region] [country] [zip] [isp]"
echo "Example: geo -a 8.8.8.8 -o city,zip,isp"
echo " -o [options] Returns Specific Geodata"
echo " -a [address] For specific ip in -s"
echo " -v Returns Version"
echo " -h Returns Help Screen"
exit
}
# Displays version number
version() {
echo "Version 1.2.0";
}
# Fetches WAN ip address
wan_search() {
curl ipv4.icanhazip.com
}
# Fetches current LAN ip address
lan_search() {
if [ $OS = "Darwin" ]; then
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
elif [ $OS = "Linux" ]; then
ip addr show | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
else
echo "OS not supported"
exit 1
fi
}
# Fetches Router ip address
router_search() {
if [ $OS = "Darwin" ]; then
netstat -rn | grep default | head -1 | awk '{print$2}'
elif [ $OS = "Linux" ]; then
ip route | grep ^default'\s'via | head -1 | awk '{print$3}'
else
echo "OS not supported"
exit 1
fi
}
# Fetches DNS nameserver
dns_search() {
if [ $OS = "Darwin" ]; then
grep -i nameserver /etc/resolv.conf |head -n1|cut -d ' ' -f2
elif [ $OS = "Linux" ]; then
cat /etc/resolv.conf | grep -i ^nameserver | cut -d ' ' -f2
else
echo "OS not supported"
exit 1
fi
}
# Fetches MAC address of
mac_search() {
if [ $OS = "Darwin" ]; then
ifconfig "$MAC" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
elif [ $OS = "Linux" ]; then
ip addr show "$MAC" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | grep -v ff:
else
echo "OS not supported"
exit 1
fi
}
# Fetches current geodata based on ip
geodata_search() {
curl -sf "http://ip-api.com/line/?fields=query,city,region,country,zip,isp"
}
# Fetches specific geodata based on args
specific_geo() {
if [ "$OPTIONS" = "all" ]; then
curl -s "http://ip-api.com/line/${ADDRESS}?fields=query,city,region,country,zip,isp"
else
curl -sf "http://ip-api.com/line/${ADDRESS}?fields=${OPTIONS}"
fi
}
# Check OS type at beginning
OS="`uname`"
# Option parsing "controller"
optspec="wlrdm:go:a:vh*:"
while getopts "$optspec" optchar
do
case "${optchar}" in
w) wan_search ;;
l) lan_search ;;
r) router_search ;;
d) dns_search ;;
m) MAC=$OPTARG mac_search;;
g) geodata_search ;;
a) ADDRESS=$OPTARG ;;
o) OPTIONS=$OPTARG specific_geo ;;
v) version ;;
h) usage ;;
*) usage ;;
esac
done
# Default to help screen for usability
if [ $# -eq 0 ];
then
usage
exit 0
fi