Easily check your IP address with Perl

Every now and then I’ll run into a problem where I need to programmatically check my IP address. Each time I’ve hand-crafted a solution, which is fine, but good programming is DRY programming, and so I finally wrote a module to do it. The module is called WWW::curlmyip because it uses the curlmyip.com service. I find the module useful and you might too.

Core Perl solution

Before we look at the module, let’s consider a Perl solution using only core Perl code. I can grab my IP address from the terminal with a single line of Perl:

$ perl -MHTTP::Tiny -e 'print HTTP::Tiny->new->get(q{http://curlmyip.com})->{content}'
121.45.140.5

Tip: if you’re on Windows use double quotes instead of singles.

Well that was easy. But notice how I didn’t have to append a newline to the output? That’s because curlmyip.com returns the IP address with a newline appended. If we want to use the IP address as an input to any other program, we’ll need to chomp that newline away. The code would then be:

$ perl -MHTTP::Tiny -E '$ip=HTTP::Tiny->new->get(q{http://curlmyip.com})->{content}; chomp $ip; say $ip'
121.45.140.5

Not so clean anymore is it? In fact it would be a stretch to call this a “one liner” at all. What about if I wanted to add exception handling, to die and print a useful error message? Once you get to this stage, it’s time to think about putting the code into a module.

Using WWW::curlmyip

The module exports a get_ip function which returns the IP address. It’s super simple to use:

use WWW::curlmyip;

my $ip = get_ip();

So far, so good. But what can you do with this information? In the past I’ve had programs check my IP address when connected to a VPN, or to TOR to confirm my real IP is masked. The other obvious use case is geolocation:

#!/usr/bin/env perl

use WWW::curlmyip;
use Geo::IP;

my $ip = get_ip();

my $geoip = Geo::IP->open('GeoLiteCity.dat', GEOIP_STANDARD);
my $record = $geoip->record_by_addr($ip);

print "You are in $record->{region_name}, $record->{country_code}\n";

In this code I retrieve my IP address and then lookup my location using the Geo::IP module from MaxMind. Saving the code as whereami.pl and running it outputs:

$ whereami.pl
You are in New York, US

The geolocation data could also be used an an input to last week’s weather script to automatically retrieve the weather forecast for your local area.

Conclusion

It’s a simple task but hopefully WWW::curlmyip makes obtaining your IP address a little easier. If your interested in Geo::IP, check out Gabor Szabo’s recent guide on how to install it. Finally, if you want to get your IP address and location in a single request, take a look at my other new module, WWW::ipinfo.


This article was originally posted on PerlTricks.com.

Tags

David Farrell

David is a professional programmer who regularly tweets and blogs about code and the art of programming.

Browse their articles

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub