#!/bin/bash # ------------------------------------------------------------------------------ # Copyright (c) 2013-2022 Geoffray Levasseur # 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. # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Download a resource using curl, wget, or fetch. # Usage: dwl [output_file] dwl() { case "$1" in --help|-h) echo "Usage: dwl [output_file]" echo "Downloads a resource using curl, wget, or fetch." echo "" echo "Arguments:" echo " url The full URL to download (http/https/ftp)." echo " output_file (Optional) Path to save the file. If omitted, prints to stdout." return 0 ;; "") echo "Error: URL argument is missing." >&2 echo "Try 'get_resource --help' for usage." >&2 return 1 ;; esac case "$1" in http://*|https://*|ftp://*) ;; *) echo "Error: '$1' does not look like a valid URL. Must start with http://, https://, or ftp://" >&2 return 1 ;; esac local url="$1" local output="$2" if command -v curl >/dev/null 2>&1; then [ -z "$output" ] && curl -sL "$url" || curl -sL -o "$output" "$url" elif command -v wget >/dev/null 2>&1; then [ -z "$output" ] && wget -qO- "$url" || wget -q -O "$output" "$url" elif command -v fetch >/dev/null 2>&1; then [ -z "$output" ] && fetch -o - "$url" || fetch -o "$output" "$url" else echo "Error: No download utility (curl, wget, or fetch) found." >&2 return 1 fi } export -f dwl # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Determine if parameter is a valid IPv4 address # Usage: isipv4 isipv4() { # Set up local variables local ip=$1 [[ -z $ip ]] && return 1 # 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='.' read -r -a ip_arr <<< "$ip" IFS=$old_ifs # 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 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 # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Determine if parameter is a valid IPv6 address # Usage: isipv6 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 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 '%s' "$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: # -h, --help Display help screen # -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) # -a, --as Display only the Autonomous System (AS) information # -R, --raw Display raw JSON response myextip() { local show_ip=false show_isp=false show_loc=false local show_coord=false show_as=false show_raw=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 ;; -R|--raw) all=false show_raw=true ;; -h|--help) printf "Fetch and display external IP information.\n\n" printf "Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] [-a|--as] [-R|--raw]\n\n" printf "Options:\n" printf "\t-h, --help\tDisplay this help screen\n" printf "\t-i, --ip\tDisplay only the external IP address\n" printf "\t-s, --isp\tDisplay only the ISP name\n" printf "\t-l, --loc\tDisplay only the location (city, region, country)\n" printf "\t-c, --coord\tDisplay only the coordinates (latitude, longitude)\n" printf "\t-a, --as\tDisplay only the Autonomous System (AS) information\n" printf "\t-R, --raw\tDisplay raw JSON response\n" return 0 ;; --) shift break ;; *) disp E "Unknown option: $1, use \"myextip --help\" to display usage." return 1 ;; esac shift done # Fetch data. Allow overriding endpoint via env var MYEXTIP_URL local MYEXTIP_URL MYEXTIP_URL=${MYEXTIP_URL:-http://ip-api.com/json/} 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 // .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" fi } export -f myextip # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # EOF