Skip to content

Instantly share code, notes, and snippets.

@chrish13
Last active July 14, 2020 23:48
Show Gist options
  • Save chrish13/a9f7caf3930c7a6a40be6f88cd0a0c0a to your computer and use it in GitHub Desktop.
Save chrish13/a9f7caf3930c7a6a40be6f88cd0a0c0a to your computer and use it in GitHub Desktop.
A bash shell script to output simple info about Wan IP Lan Ip
#!/usr/bin/env bash
# Author: Chrish
# A simple script that queries the http://ifconfig.me and fetch the public ip. Also I wanted to add here
# the default nic, ip address and default gateway.
# if you like this script please leave a comment.
# if you do not like it please leave a comment again.
# All recommendations are welcomed.
set -o errexit;
# set -o nounset;
# set -o xtrace;
function whatismyip() {
default_iface=$(ip route list | head --lines 1 | grep --perl-regexp --only-matching '\Keth[0-9]{1,2}|\Kenp\w{3}|\Kwlan[0-9]');
lanip=$(ip address show dev "${default_iface}" | grep --perl-regexp --only-matching 'inet \K[\d.]+');
default_gateway=$(ip route list | grep --perl-regexp --only-matching 'via \K[\d.]+');
wanip=$(curl --silent ifconfig.me);
while [ $# -gt 0 ]; do
case "$1" in
-v|--version)
printf "WhatIsMyIP version 0.1\n"
return 0;;
-h|--help)
printf "I need to work on a mini help\n"
return 0;;
esac
done
printf -- "%b\n" "DEVICE: \033[32m"${default_iface}"\033[0m";
printf -- "%b\n" "LAN IP: \033[32m"${lanip}"\033[0m";
printf -- "%b\n" "GATEWAY: \033[32m"${default_gateway}"\033[0m";
printf -- "%b\n" "WAN IP: \033[32m"${wanip}"\033[0m";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment