The Internet is a “network of networks,” on which all kinds of data travel in the same way: wrapped in packets formatted according to the Internet Protocol (IP). Each IP packet carries inside the numeric IP addresses of its source and destination. Internet routers use those numbers to ship each packet to the right computer.
Here are some facts about IP addresses that many Internet users don’t know, or don’t take in to proper account:
- IP addresses are all computers actually need to exchange data over the Internet1. Domain names like TechRepublic.com were created to make life for human users easier, but they are always translated to those numbers.
- IP addresses are associated to single interfaces, not whole computers. A workstation with 6 Ethernet cards, for example, can and will very likely have six different IP addresses, one per port.
- It is possible to intercept traffic for a certain IP address and translate it in real time to another one. This process, called port forwarding, is what ADSL or Wi-Fi routers normally do before sending their users’ packets out on the Internet.
- IP addresses can be static or dynamic. Wireless and most Fixed ADSL private users get a different IP address every time they connect to the Internet.
Why do IP addresses matter (especially on Linux)?
Linux systems come with piles of Free Software, already bundled or just a few clicks away, to offer all kinds of services over the Internet. I’m not talking only of the basic, popular stuff like email or websites. With Linux, you can provide to all your friends and coworkers custom social bookmarks, encrypted chat sessions, automatic remote backups, multiplayer games, video streaming, and much more, even from your home computer.
Unfortunately, there is a problem here, which is a direct corollary of the properties listed above. You may offer all those services even if none of the involved parties (including your own computer) has a domain name, but not without knowing all their IP addresses. Since all of those addresses may change at every session, what should you do? Collect and insert them manually, every time? Of course not! The solution is to have all the interested computers determine their own IP address, every time they boot, and send it by email to the other parties, all with a script. But…
How do I find my IP address on Linux?
I decided to write this post when I realized that most of the first links returned by search engines as answers to this question are simply useless. Strictly speaking, the tips in all those pages are correct. However, in our scenario of (mainly) home computers connected via some ADSL/Wi-Fi router, they will return your local, private IP address. This is an address that is only visible and valid before port forwarding, that is, for your router and all the local computers connected to it. People in other places won’t be able to send packets to that IP address (in their local network, it may even correspond to their own PC!).
What you want to know and broadcast is the (always different) public IP address that your ISP assigns to your router every time you turn it on. Here are two ways to find it from inside a script.
There is a dynamic page at ifconfig.me that extracts your current IP number from your connection request, then (click on the link to see for yourself) returns it as its own content.
Wget and Curl are Unix command line utilities to download single web pages or mirror whole websites (Curl can do much more, including filling HTML forms automatically, but we don’t need that today). w3m is a textual Web browser.
The simplest method2 to load your public IP address into a shell variable is to download that page with any of these three utilities:
MY_PUBLIC_IP_ADDRESS=`wget -qO- ifconfig.me/ip`
MY_PUBLIC_IP_ADDRESS=`w3m -no-cookie -dump ifconfig.me/ip`
Scrape other websites
The limit of the ifconfig.me approach is that… should that website go offline, it would stop working. Luckily, there are many other Web pages, like whatismyip.com or whatismyipaddress.com that will tell you your public IP address. They just need a bit of Web scraping because, unlike ifconfig.me, they contain much more text. If you don’t know what Web scraping is, please read my introduction to it. In our case, you can use Web scraping as follows:
MY_PUBLIC_IP_ADDRESS=`w3m -no-cookie -dump http://whatismyip.com | grep '\''^Your IP Address Is:'\'' | cut -c21-`
Here, the w3m text browser fetches the Web page and prints it on its standard output. Grep discards everything but the one line containing the answer you want, and cut leaves only the actual number inside MY_PUBLIC_IP_ADDRESS. At this point, you can do whatever you want with that variable in the script, for example, sending it via email to any other script, user or computer that would need to know it.
- Yes, of course Internet packets normally go to combinations of IP addresses and TCP ports. I deliberately left that part out because it wouldn’t make any difference with respect to the goal of this post.
- Source: Command Line FU