-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathipfetch-curl
More file actions
executable file
·76 lines (63 loc) · 2.37 KB
/
ipfetch-curl
File metadata and controls
executable file
·76 lines (63 loc) · 2.37 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
#!/usr/bin/env bash
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
ip_info=$(curl -sf https://api.seeip.org/geoip/$IP) || { error "Error: IP is invalid or API is down!"; }
# Remove any backslashes from the JSON response
ip_info=$(echo "$ip_info" | sed 's/\\//g')
# Extracting each field using sed and assigning them to local variables
ip=$(echo "$ip_info" | sed -nr 's/.+"ip":"([^"]+).+/\1/p')
country=$(echo "$ip_info" | sed -nr 's/.+"country":"([^"]*)".+/\1/p')
timezone=$(echo "$ip_info" | sed -nr 's/.+"timezone":"([^"]*)".+/\1/p')
latitude=$(echo "$ip_info" | sed -nr 's/.+"latitude":([^,]*).+/\1/p')
longitude=$(echo "$ip_info" | sed -nr 's/.+"longitude":([^,]*).+/\1/p')
asn=$(echo "$ip_info" | sed -nr 's/.+"asn":([^,]*).+/\1/p')
continent=$(echo "$ip_info" | sed -nr 's/.+"continent_code":"([^"]*)".+/\1/p')
city=$(echo "$ip_info" | sed -nr 's/.+"city":"([^"]*)".+/\1/p')
isp=$(echo "$ip_info" | sed -nr 's/.+"organization":"([^"]*)".+/\1/p')
region=$(echo "$ip_info" | sed -nr 's/.+"region":"([^"]*)".+/\1/p')
# Create a "flag" variable by replacing spaces with underscores in the country name
flag=$(echo "$country" | sed -r 's/ /_/g')
# 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