updated readme and final fixes for net.sh

This commit is contained in:
fatalerrors
2026-03-10 18:05:44 +01:00
parent ae90a9f4c4
commit bc8cb4a237
2 changed files with 51 additions and 30 deletions

View File

@@ -91,23 +91,32 @@ isipv4()
local ip=$1
[[ -z $ip ]] && return 1
# Start with a regex format test
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
# Start with a regex format test (four octets)
if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
local old_ifs=$IFS
IFS="."
ip=($ip)
IFS='.'
read -r -a ip_arr <<< "$ip"
IFS=$old_ifs
if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 &&
${ip[2]} -le 255 && ${ip[3]} -le 255 ]]; then
if [[ -t 1 ]]; then
disp "The given IPv4 is valid."
# Ensure each octet is between 0 and 255
local oct
for oct in "${ip_arr[@]}"; do
# Reject leading plus/minus or empty entries
if [[ -z $oct || $oct =~ [^0-9] ]]; then
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
return 1
fi
return 0
fi
fi
if [[ -t 1 ]]; then
disp "The given parameter is NOT a valid IPv4."
if (( oct > 255 )); then
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
return 1
fi
done
[[ -t 1 ]] && disp "The given IPv4 is valid."
return 0
fi
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
return 1
}
export -f isipv4
@@ -115,7 +124,7 @@ export -f isipv4
# ------------------------------------------------------------------------------
# Determine if parameter is a valid IPv4 address
# Determine if parameter is a valid IPv6 address
# Usage: isipv6 <ip_address>
isipv6()
{
@@ -146,7 +155,7 @@ urlencode() {
for (( i = 0; i < length; i++ )); do
local c="${str:i:1}"
case "$c" in
[a-zA-Z0-9.~_-]) printf "$c" ;;
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
' ') printf '+' ;;
*) printf '%%%02X' "'$c" #| cut -d' ' -f2 ;;
esac
@@ -225,22 +234,31 @@ myextip() {
shift
done
# Fetch data
local response
response=$(dwl http://ip-api.com/json/)
# Fetch data. Allow overriding endpoint via env var MYEXTIP_URL
local MYEXTIP_URL
MYEXTIP_URL=${MYEXTIP_URL:-http://ip-api.com/json/}
# Parse with jq
local response
if ! response=$(dwl "$MYEXTIP_URL"); then
disp E "Failed to fetch external IP information from $MYEXTIP_URL"
return 2
fi
# Parse with jq when available and when raw wasn't requested. The jq filter
# is tolerant to field-name differences between providers (ip-api / ipinfo).
if command -v jq >/dev/null 2>&1 && [[ "$show_raw" != true ]]; 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)
] | .[]'
--argjson isp "$show_isp" --argjson loc "$show_loc" \
--argjson coord "$show_coord" --argjson as "$show_as" '
[
(if $all or $ip then "IP Address : \(.query // .ip)" else empty end),
(if $all or $isp then "ISP : \(.isp // .org)" else empty end),
(if $all or $loc then
("Location : " + ((.city // "") + (if .city then ", " else "" end) + (if .regionName then .regionName else .region end) + (if .country then ", " + .country else "" end)))
else empty end),
(if $all or $coord then (if (.lat and .lon) then "Coordinates: \(.lat), \(.lon)" elif .loc then "Coordinates: \(.loc)" else empty end) else empty end),
(if $all or $as then "AS : \(.as // .org)" else empty end)
] | .[]'
else
[[ "$show_raw" != true ]] && disp W "jq is not installed, displaying raw JSON response."
echo "$response"