-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocduck
executable file
·88 lines (76 loc) · 3.09 KB
/
geolocduck
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
#!/bin/bash
### Simple script to get current Wan IP + Geolocation from Duckduckgo
### v1.4 - adapt to DuckDuck Answer Country only in certain conditions
### v1.3 - removed temp file and awk correction
### v1.2 - Added option to get only Wan IP (-o) and awk output (-a) if specified
### v1.1 - Added colors and myecho functions (available in 'functions' dir in the same repo)
# get script version
_VERS=$(awk '/### v/ {print $2; exit}' $basename $0)
# test internet access
nc -zw1 duckduckgo.com 443 >/dev/null 2>&1 || { echo "# No Internet Access, exiting.." && exit 1; }
while (( $# )); do
case $1 in
-o|--only-ip) _ONLYIP=1; shift ;;
-h|--help) echo "Usage: $0 [-o|--only-ip]"
echo " -o|--only-ip Get only the IP address"
echo " -h|--help Show this help"
exit 0
;;
-a|--awk) _AWK=1; shift ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
# Load myecho for nicer output
if ! type _MYECHO >/dev/null 2>&1; then
if [ -f "/etc/profile.d/01-myecho-colors.sh" ]; then
source /etc/profile.d/01-myecho-colors.sh >/dev/null 2>&1
elif [ -f "$HOME/01-myecho-colors.sh" ]; then
source "$HOME/01-myecho-colors.sh" >/dev/null 2>&1
else
echo "Nice Output - Install 'myecho' function (in homedir: 01-myecho-colors.sh)"
cd "$HOME"
curl -s -LO https://raw.githubusercontent.com/zephxs/bash/master/functions/01-myecho-colors.sh
head -20 01-myecho-colors.sh |grep -q "^_MYECHO () {" && echo "myecho installed!" || { echo "myecho install failed, exit.."; exit 1; }
source "$HOME/01-myecho-colors.sh" >/dev/null 2>&1
fi
fi
### Main
# Query duckduckgo.com for "ip address" terms
_TMPRESULT=$(curl -s "https://duckduckgo.com/?q=ip+address" | awk -F'Your IP address is |</a>","AnswerType":"ip"' '{print $2}' | sed -r 's/<a href.*>//; s/\)//; s/\(//; s/\\u00f4/ô/')
# Set Vars for proper display
if [ $(echo "$_TMPRESULT" |wc -w) -le 4 ]; then
_IP=$(echo "$_TMPRESULT" |awk '{print $1}')
_COUNTRY=$(echo "$_TMPRESULT" |awk -F', | ' '{print $(NF)}')
else
_IP=$(echo "$_TMPRESULT" |awk '{print $1}')
_ZIPCODE=$(echo "$_TMPRESULT" |awk '{print $NF}')
_CITY=$(echo "$_TMPRESULT" |awk -F' in |, ' '{print $2}')
_PROVINCE=$(echo "$_TMPRESULT" |awk -F', ' '{print $2}')
_COUNTRY=$(echo "$_TMPRESULT" |awk -F', | ' '{print $(NF-1)}')
fi
# If only IP is requested, show it and exit
if [[ $_ONLYIP -eq 1 ]]; then
echo $_IP
exit 0
fi
_AWKRESULT () {
echo "########## IP DuckDuck Geoloc ### $_VERS ##########"
echo "$_TMPRESULT" |awk '{print "# IP: " $1"\n# City: " $3"\n# ZipCode: " $NF"\n# Province: " $4"\n# Country: " $(NF-1)}' |sed 's/,//'
}
_MYRESULT () {
if [ -z "$_ZIPCODE" ]; then
_MYECHO -t "IP DuckDuck Geoloc ### $_VERS"
_MYECHO -e "IP" && echo $_IP
_MYECHO -e "Country" && echo $_COUNTRY
else
_MYECHO -t "IP DuckDuck Geoloc ### $_VERS"
_MYECHO -e "IP" && echo $_IP
_MYECHO -e "City" && echo $_CITY
_MYECHO -e "ZipCode" && echo $_ZIPCODE
_MYECHO -e "Province" && echo $_PROVINCE
_MYECHO -e "Country" && echo $_COUNTRY
fi
}
# Show results
if [[ $_AWK -eq 1 ]]; then _AWKRESULT; exit 0; fi
if ! type _MYECHO >/dev/null 2>&1; then _AWKRESULT; else _MYRESULT; fi