197 lines
6.8 KiB
Bash
197 lines
6.8 KiB
Bash
#!/bin/bash
|
|
# ------------------------------------------------------------------------------
|
|
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
# Protected by the BSD3 license. Please read bellow for details.
|
|
#
|
|
# * Redistribution and use in source and binary forms,
|
|
# * with or without modification, are permitted provided
|
|
# * that the following conditions are met:
|
|
# *
|
|
# * Redistributions of source code must retain the above
|
|
# * copyright notice, this list of conditions and the
|
|
# * following disclaimer.
|
|
# *
|
|
# * Redistributions in binary form must reproduce the above
|
|
# * copyright notice, this list of conditions and the following
|
|
# * disclaimer in the documentation and/or other materials
|
|
# * provided with the distribution.
|
|
# *
|
|
# * Neither the name of the copyright holder nor the names
|
|
# * of any other contributors may be used to endorse or
|
|
# * promote products derived from this software without
|
|
# * specific prior written permission.
|
|
# *
|
|
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
# * OF SUCH DAMAGE.
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Determine if parameter is a valid IPv4 address
|
|
# Usage: isipv4 <ip_address>
|
|
isipv4()
|
|
{
|
|
# Set up local variables
|
|
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
|
|
local old_ifs=$IFS
|
|
IFS="."
|
|
ip=($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."
|
|
fi
|
|
return 0
|
|
fi
|
|
fi
|
|
if [[ -t 1 ]]; then
|
|
disp "The given parameter is NOT a valid IPv4."
|
|
fi
|
|
return 1
|
|
}
|
|
export -f isipv4
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Determine if parameter is a valid IPv4 address
|
|
# Usage: isipv6 <ip_address>
|
|
isipv6()
|
|
{
|
|
local ip="$1"
|
|
local regex='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$'
|
|
if [[ $ip =~ $regex ]]; then
|
|
if [[ -t 1 ]]; then
|
|
disp "The given IPv6 is valid."
|
|
fi
|
|
return 0
|
|
fi
|
|
if [[ -t 1 ]]; then
|
|
disp "The given parameter is not a valid IPv6."
|
|
fi
|
|
return 1
|
|
}
|
|
export -f isipv6
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Encode a string so it can be used as a URL parameter
|
|
# Usage: urlencode <string>
|
|
urlencode() {
|
|
local LANG=C
|
|
local str="$*"
|
|
local length="${#str}"
|
|
for (( i = 0; i < length; i++ )); do
|
|
local c="${str:i:1}"
|
|
case "$c" in
|
|
[a-zA-Z0-9.~_-]) printf "$c" ;;
|
|
' ') printf '+' ;;
|
|
*) printf '%%%02X' "'$c" #| cut -d' ' -f2 ;;
|
|
esac
|
|
done
|
|
}
|
|
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
|
|
local show_coord=false show_as=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
|