How to Get Your Public IP in a Linux Bash Script

Configurare noua (How To)

Situatie

You can get your external IP from a website with the curl command like “curl -s ifconfig.me”. However, websites can change. For a more reliable answer, use the dig command instead like “dig @resolver1.opendns.com myip.opendns.com +short” to get your IP from a DNS server.

Solutie

Internal and External IP Addresses

Internet Protocol addresses are used to identify devices on a network. They’re a unique label on the network, assigned to a single device. If another device wants to send data to that device, it can do so using its IP address.

Your broadband modem has a network router built into it that directs the network traffic around the network from device to device. It’s also the local authority that allocates IP addresses to devices when they join the network. It also maintains a table of network names and IP addresses. This allows you to give meaningful names to the computers on your network, because—for humans—names are easier to work with than lists of numbers.

Devices on the internet also have an IP address. Some of them have names too, such as websites. Domain Name Service providers look up website domain names and swap them for IP addresses, automatically.

Your broadband modem is given its own internet-facing or external IP address by your Internet Service Provider (ISP). Regardless of the number of devices you might have in your home that are connected to the internet, their combined traffic all goes out through that single IP address.

If you’re out of town and want to connect to a service you’ve got running on one of the computers in your home, you’ll need to use your external IP address to do so. Your router will need to be set up to route your connection request to the appropriate device inside your home, of course.

Unless you pay a little extra to your ISP each month for a static IP address your external IP address may change from time to time. Rebooting your broadband modem might well result in your getting a different external IP address. So if you need to know your external IP address, you can’t just check it once and store it. You’ll need to determine periodically what it is.

Finding Your External IP Address

To discover your external IP address means talking to something that is outside of your network. In other words, accessing something on the internet that can give us the information we want. We need to peer into the void and see what’s looking back at us. And then ask it for our external address.

There are two ways we can do this. One way involves websites. There are a lot of websites that will show you what your external IP address is, and a bunch of other information too. You can access these in your browser, or use a command-line tool like curl that can make HTTPS requests.

The other way is to use a dedicated command like dig. The dig command interrogates DNS servers to retrieve information.

Using a Browser

Of course, using a browser isn’t a script-friendly way to obtain your external IP address. But looking at a website that delivers that service can give us useful information. We used to  recommend ip4.me but the site hasn’t been updated to HTTPS. It still uses the older, insecure HTTP. The site still works, but there are better alternatives now.

The ifconfig.me site provides a good set of information.

The ifconfig.me website displaying an external IP address

This is reporting our external IP as 178.238.11.140. Scrolling down through the webpage you’ll find a list of commands that you can use to retrieve information from the site.

The ifconfig.me website displaying a et of cURLcommands

The examples they give all use curl to interrogate the site. So let’s take a look at using curl.

Using curl

On our test machines, Fedora 37 already had curl installed. We needed to install it on our Ubuntu and Manjaro computers.

To install it on Ubuntu type:

sudo apt install curl

Installing curl on Ubuntu

The command on Manjaro is:

sudo pacman -S curl

Installing curl on Manjaro

We can try this out with the first command listed on the ifconfig.me webpage.

curl ifconfig.me

Fetching an external IP address using the ifconfig.me website's default action

Our external IP address is retrieved and displayed in the terminal window. The output is bare-bones. There isn’t even a newline character printed after the string. The command prompt is butted right up against the IP address.

This command works because returning the IP address is the default action of the website. If the default action ever changes, we might get a different result returned to us. To cater to this, we can specify we are requesting our IP address by adding the “ip” identifier to the URL.

curl ifconfig.me/ip

Fetching an external IP address using the ifconfig.me website with the ip specfier

This returns the IP address as before.

This illustrates the problem with using a website as the source of your IP address. Websites can close down or they can change the way they operate, or the format of the returned information. These changes will make scripts that depend on these sites to either fail or behave unpredictably.

Using a reputable and dependable resource like a DNS server is a more robust way to obtain your external IP. To query a DNS server we need to use the dig command.

Using the dig Command

This time, dig was installed on Fedora and Ubuntu, and we only had to install it on Manjaro.

The command is the usual pacman command, so no surprises there, but the package name is not what you might expect.

sudo pacman -S bind-tools

Installing dig on Manjaro

To use dig to discover our external IP address, we need to point it to a DNS server. We’re using the OpenDNS server, which is provided by Cisco.

We need to specify the name of the DNS server we want to use, preceded by an at sign “@.” We also need to name the record type we wish to retrieve. In this case it is “myip.”  The +short option ensures we get a terse response, and not a verbose one.

dig @resolver1.opendns.com myip.opendns.com +short

Retrieving an external IP address from a DNS server with the dig command

This time, our IP address has a newline character printed after it. As we’ll see, this is printed after the IP address string, it isn’t an integral part of the string itself.

Using These in a Script

There are many reasons why you might want to know your external IP address from inside a script. Perhaps you have a script that monitors whether your external IP address has changed, and it notifies you when that occurs. Or perhaps a server notifies its clients when its address changes. Whatever your reasons, we can implement the commands we’ve used on the command line inside a script quite easily.

To retrieve our external IP address and assign it to a variable, we only need to wrap the command inside a command substitution, $(...) , and assign it to a variable, like this:

variable=$(...)

The command inside the parentheses is executed and the return value is substituted for the expression. In this example, the command is simplified to “variable=return value.”

Here it is in a script. We’ve added the -s (silent) option to the curl command to prevent it from reporting the progress of the data retrieval.

#!/bin/bash

extaddr=$(curl -s ifconfig.me)

echo "The external IP address is $extaddr (from cURL)"

Copy this script to an editor, and save it as “getex1.sh”, and make it executable with the chmod command.

chmod +x getex1.sh

Making a script executable with chmod

Let’s execute the script and see what we get.

./getex1.sh

Running the getex1.sh script

To do the same with the more robust option of using a DNS server rather than a website, all we need to do is substitute the curl command with the dig one.

#!/bin/bash

extaddr=$(dig @resolver1.opendns.com myip.opendns.com +short)

echo "The external IP address is $extaddr (from dig)"

Save this as script “getex2.sh” and make it executable with chmod.

chmod +x getex2.sh

Making a script executable with chmod

Let’s execute this script.

./getex2.sh

Running the getex2.sh script

We can see from the output of the two scripts that despite the command dig printing a newline character on the command line, in the script there is no newline added to the extaddr variable.

Go For Dependability

It’s always safer to use a recognized service that is reputable and has a predictable output format than to use an “unofficial” website. Like everything else on the internet, have a good look at who you’re getting your information from.’

Tip solutie

Permanent

Voteaza

(10 din 16 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?