-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathipfetch-wget
More file actions
executable file
·79 lines (65 loc) · 2.3 KB
/
ipfetch-wget
File metadata and controls
executable file
·79 lines (65 loc) · 2.3 KB
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
#!/usr/bin/env bash
tempfile=`mktemp -u /tmp/ipfetch.XXXXXX`
IP=""
usage() {
cat <<- _end_of_usage
Usage: ipfetch [OPTIONS]...
Neofetch like tool that can lookup IPs.
Options:
-h print this text and exit
-ip <IP> ip address to fetch (fetches own ip by default)
_end_of_usage
}
error() {
echo "$1" && exit 1
}
parse_args() {
# if no arguments passed, use current ip
[ $# -eq 0 ] && return 0
while [ $# -ne 0 ]
do
case "$1" in
"-h") usage && exit 0 ;;
"-ip") IP=$2 && shift ;;
*) error "Unknown argument $1!" ;;
esac
shift >/dev/null 2>&1
done
}
ipfetch() {
# if no ip is passed, the IP variable is empty, so the api link will work as expected
wget https://api.seeip.org/geoip/$IP -O "$tempfile" >/dev/null 2>&1 || error "IP is invalid or API is down!"
# delete backslashes
sed -i 's/\\//g' "$tempfile"
# Uses sed to extract json data
# this SHOULD be done more cleanly in a for
local ip=`sed -nr 's/.+"ip":"([^"]+).+/\1/p' "$tempfile"`
local country=`sed -nr 's/.+"country":"([^"]*).+/\1/p' "$tempfile"`
local timezone=`sed -nr 's/.+"timezone":"([^"]*).+/\1/p' "$tempfile"`
local latitude=`sed -nr 's/.+"latitude":([^,]*).+/\1/p' "$tempfile"`
local longitude=`sed -nr 's/.+"longitude":([^,]*).+/\1/p' "$tempfile"`
local asn=`sed -nr 's/.+"asn":([^,]*).+/\1/p' "$tempfile"`
local continent=`sed -nr 's/.+"continent_code":"([^"]*).+/\1/p' "$tempfile"`
local city=`sed -nr 's/.+"city":"([^"]*).+/\1/p' "$tempfile"`
local isp=`sed -nr 's/.+"organization":"([^"]*).+/\1/p' "$tempfile"`
local region=`sed -nr 's/.+"region":"([^"]*).+/\1/p' "$tempfile"`
local flag=`echo "$country" | sed -r 's/ /_/g'`
rm "$tempfile"
# All of those \033 you see are cursor positions.
#Here is a good tutorial for it: https://thoughtsordiscoveries.wordpress.com/2017/04/26/set-and-read-cursor-position-in-terminal-windows-and-linux/
echo -e "\n`cat /usr/share/ipfetch/$flag`\
\033[9D\033[s \033[9A Ip: $ip\
\033[u\033[8A Continent: $continent\
\033[u\033[7A Country: $country\
\033[u\033[6A Region: $region\
\033[u\033[5A City: $city\
\033[u\033[4A ISP: $isp\
\033[u\033[3A Timezone: $timezone\
\033[u\033[2A Latitude: $latitude\
\033[u\033[1A Longitude: $longitude\
\033[u ASN: $asn\
\033[u \033[10A -------------------- \
\033[u \n"
}
parse_args "$@"
ipfetch