Last active
January 15, 2025 07:59
-
-
Save ostechnix/09259a3da0e7190775d16aadef14580b to your computer and use it in GitHub Desktop.
Show IP Address - A BASH Script to Show Private and Public IP Address Information in Linux and Unix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ------------------------------------------------------------------ | |
# Script Name: showipaddr.sh | |
# Description: A Bash Script to display Private and | |
# Public IP address details in Linux and Unix. | |
# Website: https://gist.github.com/ostechnix | |
# Version: 1.0 | |
# Usage: chmod +x showipaddr.sh | |
# ./showipaddr.sh | |
# ------------------------------------------------------------------ | |
# Clear the screen | |
clear | |
# Function to check if jq is installed | |
check_jq_installed() { | |
if ! command -v jq &> /dev/null; then | |
echo "It appears that 'jq' is not installed. Please install 'jq' package to run this script." | |
echo | |
echo "You can usually install 'jq' using:" | |
echo | |
echo " sudo apk add jq # On Alpine Linux" | |
echo " sudo pacman -S jq # On Arch Linux/EndeavourOS/Manjaro" | |
echo " sudo apt-get install jq # On Debian/Ubuntu" | |
echo " sudo yum install jq # On RedHat/CentOS" | |
echo " sudo dnf install jq # On Fedora" | |
echo " sudo zypper install jq # On SUSE/openSUSE" | |
echo " brew install jq # On macOS (with Homebrew)" | |
echo | |
exit 1 | |
fi | |
} | |
# Check if jq is installed | |
check_jq_installed | |
# Get hostname | |
hostname=$(hostname) | |
# Function to display public IP information | |
display_public_ip() { | |
local public_ip_info=$(curl -s http://ip-api.com/json) | |
local public_ip=$(echo "$public_ip_info" | jq -r '.query') | |
local city=$(echo "$public_ip_info" | jq -r '.city') | |
local region=$(echo "$public_ip_info" | jq -r '.regionName') | |
local country=$(echo "$public_ip_info" | jq -r '.country') | |
local lat=$(echo "$public_ip_info" | jq -r '.lat') | |
local lon=$(echo "$public_ip_info" | jq -r '.lon') | |
local isp=$(echo "$public_ip_info" | jq -r '.isp') | |
# echo "---------------------------------------" | |
# echo "Public IP Information:" | |
# echo "IP Address: $public_ip" | |
# echo "ISP: $isp" | |
# echo "City: $city" | |
# echo "Region: $region" | |
# echo "Country: $country" | |
# echo "Coordinates: $lat, $lon" | |
# echo "---------------------------------------" | |
echo "------------------------------------------------------" | |
echo "Public IP Information:" | |
printf "%-20s %s\n" "IP Address" ": $public_ip" | |
printf "%-20s %s\n" "ISP" ": $isp" | |
printf "%-20s %s\n" "City" ": $city" | |
printf "%-20s %s\n" "Region" ": $region" | |
printf "%-20s %s\n" "Country" ": $country" | |
printf "%-20s %s\n" "Coordinates" ": $lat, $lon" | |
echo "------------------------------------------------------" | |
} | |
# Function to display local network information | |
display_local_ip() { | |
local local_ipv4=$(hostname -I | awk '{print $1}') | |
local local_ipv6=$(ip -6 addr show | grep 'inet6' | awk '{print $2}' | grep -v '^::1/' | head -n 1) | |
local router_ip=$(ip route | grep default | awk '{print $3}') | |
local dns_server=$(grep nameserver /etc/resolv.conf | cut -d ' ' -f 2) | |
# echo "---------------------------------------" | |
# echo "Local Network Information:" | |
# echo "Hostname: $hostname" | |
# echo "Local IPv4 Address: $local_ipv4" | |
# echo "Local IPv6 Address: $local_ipv6" | |
# echo "Router IP Address: $router_ip" | |
# echo "DNS Server: $dns_server" | |
# echo "---------------------------------------" | |
echo "------------------------------------------------------" | |
echo "Local Network Information:" | |
printf "%-20s %s\n" "Hostname" ": $hostname" | |
printf "%-20s %s\n" "Local IPv4 Address" ": $local_ipv4" | |
printf "%-20s %s\n" "Local IPv6 Address" ": $local_ipv6" | |
printf "%-20s %s\n" "Router IP Address" ": $router_ip" | |
printf "%-20s %s\n" "DNS Server" ": $dns_server" | |
echo "------------------------------------------------------" | |
} | |
# Prompt user to choose what information to display | |
echo "Select the information to display:" | |
echo "1. Local IP" | |
echo "2. Public IP" | |
echo "3. Both Local and Public IP" | |
echo "Enter your choice (1/2/3):" | |
read choice | |
# Display the chosen information | |
case $choice in | |
1) | |
display_local_ip | |
;; | |
2) | |
display_public_ip | |
;; | |
3) | |
display_local_ip | |
display_public_ip | |
;; | |
*) | |
echo "Invalid choice. Please enter 1, 2, or 3." | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment