implemented showextip

This commit is contained in:
fatalerrors
2026-03-10 12:01:45 +01:00
parent 0a4206b890
commit 1484b004be

View File

@@ -108,5 +108,88 @@ export -f urlencode
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Fetch and display external IP information
# Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord]
# If no option is provided, all information will be displayed.
# Options:
# -i, --ip Display only the external IP address
# -s, --isp Display only the ISP name
# -l, --loc Display only the location (city, region, country)
# -c, --coord Display only the coordinates (latitude, longitude)
myextip() {
local show_ip=false show_isp=false show_loc=false show_coord=false
local all=true
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
-i|--ip)
show_ip=true
all=false
;;
-s|--isp)
show_isp=true
all=false
;;
-l|--loc)
show_loc=true
all=false
;;
-c|--coord)
show_coord=true
all=false
;;
-a|--as)
show_as=true
all=false
;;
-h|--help)
printf "Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] [-a|--as]\n"
printf "Fetch and display external IP information.\n\n"
printf "Options:\n"
printf " -i, --ip Display only the external IP address\n"
printf " -s, --isp Display only the ISP name\n"
printf " -l, --loc Display only the location (city, region, country)\n"
printf " -c, --coord Display only the coordinates (latitude, longitude)\n"
printf " -a, --as Display only the Autonomous System (AS) information\n"
return 0
;;
--)
shift
break
;;
*)
disp E "Unknown option: $1"
return 1
;;
esac
shift
done
# Fetch data
local response
response=$(curl -s http://ip-api.com/json/)
# Parse with jq
if command -v jq >/dev/null 2>&1; then
echo "$response" | jq -r --argjson all "$all" --argjson ip "$show_ip" \
--argjson isp "$show_isp" --argjson loc "$show_loc" \
--argjson coord "$show_coord" --argjson as "$show_as" '
[
(if $all or $ip then "IP Address : \(.query)" else empty end),
(if $all or $isp then "ISP : \(.isp)" else empty end),
(if $all or $loc then "Location : \(.city), \(.regionName), \(.country)" else empty end),
(if $all or $coord then "Coordinates: \(.lat), \(.lon)" else empty end),
(if $all or $as then "AS : \(.as)" else empty end)
] | .[]'
else
echo "$response"
fi
}
export -f myextip
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# EOF