-
-
Save juanpagfe/beeca4dc57fe6102296f3ae71d9eea3a to your computer and use it in GitHub Desktop.
Bash script to get the private and public IP address per network interface
This file contains hidden or 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
| #!/bin/bash | |
| # A command-line tool to get the private and public ip address per network | |
| # interface for Mac and Linux. | |
| # | |
| # Dependencies: | |
| # - MacOS: ifconfig | |
| # - Linux: ip | |
| # | |
| # Installation: | |
| # Only move the directory to a bin directory depending on where you want to | |
| # have the script available. | |
| # | |
| # In the gist page, copy the raw url, then: | |
| # | |
| # curl -L https://gist.githubusercontent.com/juanpagfe/{GIST_HASH}/raw/{GIST_VERSION/ips -o ~/.local/bin/ips | |
| # chmod a+x ~/.local/bin/ips | |
| # | |
| # Usage: | |
| # The script can receive network interfaces as positional arguments or it will | |
| # get them by itself if you want a result for all NICs | |
| # | |
| # Usage with a NIC: | |
| # ips en0 | |
| # | |
| # Usage without nic: | |
| # ips | |
| # Gets the operating system name | |
| unameOut="$(uname -s)" | |
| case "${unameOut}" in | |
| Linux*) export OS=Linux;; | |
| Darwin*) export OS=Mac;; | |
| CYGWIN*) export OS=Cygwin;; | |
| MINGW*) export OS=MinGw;; | |
| *) export OS="UNKNOWN:${unameOut}" | |
| esac | |
| usage() { | |
| echo -e "Tool to get the local and public IP address per network interface. You can choose the NIC as well giving it as an argument\n" | |
| echo "Usage: $0 [options] <network-interface-name>" | |
| echo "Options:" | |
| echo " -h, --help Show this help message" | |
| } | |
| wargs=false | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -h|--help) | |
| usage | |
| exit 0 | |
| ;; | |
| *) | |
| NICS=("$@") | |
| wargs=true | |
| shift | |
| break | |
| ;; | |
| esac | |
| done | |
| if [[ $# -eq 0 && -z "$NICS" ]]; then | |
| if [[ "$OS" = "Linux" ]]; then | |
| NICS=($(ip addr list | awk -F': ' '/^[0-9]/ {print $2}')) | |
| elif [[ "$OS" = "Mac" ]]; then | |
| NICS=($(ifconfig | pcregrep -M -o '^[^\t:]+(?=:([^\n]|\n\t)*status: active)')) | |
| else | |
| NICS=("$@") | |
| fi | |
| fi | |
| first=true | |
| for nic in "${NICS[@]}"; do | |
| if [[ "$nic" != "lo" && "$nic" != "lo0" ]]; then | |
| if [[ "$OS" = "Linux" ]]; then | |
| ip=$(ip -4 addr show $nic | grep -oP "(?<=inet ).*(?=/)") | |
| elif [[ "$OS" = "Mac" ]]; then | |
| ip=$(ifconfig $nic | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -1 | awk '{ print $2 }') | |
| fi | |
| if [[ ! -z $ip ]]; then | |
| pubip=$(curl --interface $nic -s ipinfo.io/ip; echo) | |
| if [ "$first" = true ]; then | |
| first=false | |
| echo "------------------------------------" | |
| fi | |
| echo "$nic: $ip | $pubip" | awk -F '[:| ]+' '{print "Network Interface: " $1 "\nLocal IP: " $2 "\nPublic IP: " $3}' | |
| echo "------------------------------------" | |
| else | |
| if $wargs; then | |
| echo "The network interface '$nic' does not have a local ip address or does not exist." | |
| exit 1 | |
| fi | |
| fi | |
| fi | |
| done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment