#!/usr/bin/perl -w
# myip - List the external ip address of the current computer as seen by outside servers
# Copyright 2010 Peter Stuifzand
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
use 5.010;
use LWP::Simple 'get';
use HTML::TreeBuilder::XPath;
use URI;
use Getopt::Std;
our $VERSION = '0.99';
$Getopt::Std::STANDARD_HELP_VERSION = 1;
my %opts;
getopts('hvu:', \%opts);
my $tools_url = $opts{u} || 'http://stuifzand.eu/tools/';
my $myip_url = myip_url($tools_url);
my ($ip, @extras) = get_my_ip($myip_url);
say $ip;
if ($opts{v}) {
say for @extras;
}
sub HELP_MESSAGE {
print <<"USAGE";
Usage: myip [OPTION]...
List the external ip address of the current computer as seen by outside servers.
-u url use another tools url
-v verbose
-h this help screen
USAGE
}
sub VERSION_MESSAGE {
print <<"VERSION";
myip $VERSION
Copyright (C) 2010 Peter Stuifzand
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Peter Stuifzand
VERSION
}
sub get_my_ip {
my ($myip_url) = @_;
my $text = get($myip_url);
if (!$text) {
die "Can't get text: $@";
}
my @lines = split /\n/, $text;
chomp for @lines;
return @lines;
}
sub myip_url {
my ($baseurl) = @_;
if (!ref($baseurl)) {
$baseurl = URI->new($baseurl);
}
my $text = get $baseurl;
if (!$text) {
die "Can't get text: $@";
}
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse_content($text);
for ($tree->findnodes(q{//ul[@class='tools']/li})) {
my $url = URI->new($_->findvalue(q{a[@class='myip']/@href}));
return $url->abs($baseurl);
}
die "Can't find myip url";
}