Home Linux Networking How To Display Local And Public IP Address In Linux Using Showipaddr Script

How To Display Local And Public IP Address In Linux Using Showipaddr Script

Instantly Find Your Local and Public IP in Linux (Super Easy!)

By sk
830 views

Need a quick and easy way to find your IP addresses in Linux? In this article, we'll guide you through two simple yet useful scripts named showipaddr that will help you display your private (local) and public IP addresses effortlessly in Linux.

Of course, you can always use the built-in commands like "ip" or "nmcli" to retrieve your IP addresses, but these commands may provide you with more information than you need. If you're looking for a quick and straightforward way to get just your local and public IP addresses, you can use these scripts!

Here's what you can do with these scripts:

  • Check your local network information: Show your Linux system's hostname, IPv4 and IPv6 addresses, Gateway (Router) address, and DNS server details.
  • Discover your public IP address: Display your public IP address, along with other useful details such as ISP, City, Region, Country, and co-ordinates (latitude and longitude).

Both scripts are freely available in GitHub. You can download them, update them as per your needs and run them to instantly get your local and public IP information.

First, we will see how to use Showipaddr BASH script to reveal the IP address in Linux.

Show IP Address Information using BASH Script

The BASH script showipaddr.sh is designed to display both local and public IP information in Linux and Unix-like systems. By default, the script uses ip-api.com to fetch the public IP information. However, you can modify the script to use other services like ipinfo.io, ifconfig.me, etc.

In order to use this script, you should have the following dependencies installed on your Linux system:

  • Bash Shell: The script is written in Bash, which is typically the default shell on most Linux distributions.
  • Curl: This tool is used to fetch public IP information from the internet (curl -s http://ip-api.com/json). Most Linux distributions come with curl pre-installed. If not, it can be installed using the package manager (like apt for Debian/Ubuntu, yum for CentOS, etc.).
  • jq: This is a lightweight and flexible command-line JSON processor. It is used to parse JSON data returned by the ip-api.com service. It might not be pre-installed on all systems, and you can install it using your distribution's package manager.
  • Internet Connection: To fetch public IP information from ip-api.com.

Let us see how to instantly get both private and public IP information using Showipaddr script:

1. Download the script from our GitHub page:

$ git clone https://gist.github.com/09259a3da0e7190775d16aadef14580b.git showipaddr

This will download the showipaddr.sh in a local directory called showipaddr. You can use any name to this folder.

2. Cd into the directory:

$ cd showipaddr/

3. Make the script executable using chmod command:

$ chmod +x showipaddr.sh

4. Move the script to your $PATH:

$ sudo mv showipaddr.sh /usr/local/bin/showipaddr

5. Now, run the script using command:

$ showipaddr

The script will prompt you choose any one of the following choices:

  • Show local IP information only,
  • Display Public IP details only,
  • Display both private and public IP addresses.

Enter your choice by typing the relevant number. For instance, typing 1 will display your local IP address, typing 2 will print the public IP details and 3 will get you both details.

Here is the sample output from my Ubuntu 22.04 system after I input number 3:

Select the information to display:
1. Local IP
2. Public IP
3. Both Local and Public IP
Enter your choice (1/2/3):
3
------------------------------------------------------
Local Network Information:
Hostname             : ubuntu2204
Local IPv4 Address   : 192.168.1.40
Local IPv6 Address   : fe80::6c26:bff:fe8e:338c/64
Router IP Address    : 192.168.1.101
DNS Server           : 127.0.0.53
------------------------------------------------------
------------------------------------------------------
Public IP Information:
IP Address           : 49.37.192.210
ISP                  : Reliance Jio Infocomm Limited
City                 : Chennai
Region               : Tamil Nadu
Country              : India
Coordinates          : 12.8996, 80.2209
------------------------------------------------------
Display IP Address in Linux using showipaddr Bash Script
Display IP Address in Linux using showipaddr Bash Script

How Showipaddr.sh Script Works?

The script performs the following tasks:

1. Check if jq is installed:

  • The script first whether jq is installed or not in your Linux system. If jq is not installed, the script will exit and print the jq installation instructions for various Linux distributions.

2. Public IP Information Retrieval:

  • The script uses curl to make a request to https://ip-api.com/json. This URL returns information about the public IP address of the system in JSON format.
  • The jq tool then parses this JSON data to extract details such as the public IP address, ISP, city, region, country, and geographical coordinates.

3. Local Network Information Retrieval:

  • hostname -I: This command fetches the local IP addresses of the machine. It typically returns the IPv4 address.
  • ip -6 addr show: This command lists all IPv6 addresses. The script filters this to get the local IPv6 address, excluding loopback and link-local addresses.
  • ip route: This is used to get the default gateway (router IP address) of the machine.
  • grep nameserver /etc/resolv.conf: This fetches the DNS server information from the resolv.conf file.

4. User Interaction:

  • The script prompts the user to choose whether to display local IP, public IP, or both. Depending on the user's choice, it calls the respective function to display the relevant information.

5. Output Formatting:

  • The script uses echo and printf commands to format and display the information in a readable and structured format.

This script is a useful tool for quickly retrieving and displaying network information on a Linux system. It combines various Linux commands and utilities to gather and present this data.

If you don't like or prefer the BASH script, you could use our Python script which does the same operation.

Display IP Address in Linux using Python Script

The Python script to unveil the IP address in Linux is called showipaddr.py. It exactly does the same as the Bash script. The only difference is it is written in Python.

The required dependencies to use this script are given below:

  • Python: The programming language in which the script is written. You need Python installed on your Linux system to run this script.
  • requests Library: A Python library used for making HTTP requests. This script uses requests to fetch public IP information from the ip-api.com service. To install this library, you can run pip install requests in your terminal.

JSON Processor:

  • jq: While not a Python dependency, jq is a lightweight and flexible command-line JSON processor. The script checks if jq is installed in the system to ensure compatibility with JSON processing, although it does not directly use jq in the code.

Linux Utilities:

  • hostname: Used to get the hostname and local IP addresses.
  • ip: Used to get router IP address and local IPv6 address.
  • grep, awk: Utilities for text processing in shell commands.

Here is the steps to display IP address in Linux using Showipaddr python script:

1. Download the script using command:

$ git clone https://gist.github.com/27276f43e980aa3a468c4b39680a68ee.git showipaddr

This command will download the script in a local folder called 'sowipaddr'.

2. Cd into that directory:

$ cd showipaddr/

3. Run the script:

$ python3 showipaddr.py

Similar to our previous Bash script, it will also give you three choices. Type number 1 for displaying private IP, type 2 for Public IP and type 3 for displaying both private and public IP addresses.

Show Local and Public IP Information in Linux using showipaddr Python Script
Show Local and Public IP Information in Linux using showipaddr Python Script

How the Script Works

The script performs several tasks:

1. Check for jq:

  • It first checks if jq is installed. If not, it shows instructions for installing jq on various systems.

2. Fetch IP Information:

  • Local IP Details: The script uses Linux commands (executed via Python's subprocess.getoutput()) to fetch the local hostname, IPv4, IPv6, router IP, and DNS server.
  • Public IP Details: It uses the requests library to send an HTTP GET request to ip-api.com. The response, which is in JSON format, contains details about the public IP, like ISP, city, country, etc.

3. User Interaction:

  • Clears the screen.
  • Prompts the user to choose what information to display (local IP, public IP, or both).
  • Based on the user's choice, it fetches and displays the relevant information.

4. Displaying Information:

  • Formats and prints the information in a readable table format.

Conclusion

Whether you're a tech enthusiast or a casual user, knowing your local and public IP addresses in a Linux environment can be invaluable for troubleshooting network issues, and setting up servers. Using these two scripts, you can instantly get either the local IP information or public IP details or both.

Resources:


Related Read:


You May Also Like

2 comments

James Horn December 30, 2023 - 8:56 pm

While running the script(replaced information with “x”):
Select the information to display:
1. Local IP
2. Public IP
3. Both Local and Public IP
Enter your choice (1/2/3):
3
——————————————————
Local Network Information:
Hostname : xxxxxxx
Local IPv4 Address : xxx.xxx.xxx.x
Local IPv6 Address : xxxx:xxxx:xxxx:xxxx:xxxx/64
Router IP Address : 192.168.254.254
DNS Server : 127.0.0.1
——————————————————
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 6
——————————————————
Public IP Information:
IP Address :
ISP :
City :
Region :
Country :
Coordinates : ,
——————————————————

Reply
James Horn December 30, 2023 - 9:17 pm

On the last post, figured out the issue. I was running PiHole and it was blocking the query. I have the fix. Thank you.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More