Skip to content

Instantly share code, notes, and snippets.

@vshymanskyy
Created May 22, 2013 11:15
Show Gist options
  • Save vshymanskyy/5626831 to your computer and use it in GitHub Desktop.
Save vshymanskyy/5626831 to your computer and use it in GitHub Desktop.
A better way to get external ip address. Checks external ip address on random services until 2 of them return same result. (Needs wget utility)
#!/usr/bin/perl
# A better way to get external ip address
# Checks external ip address on random services until 2 of them return same result.
# Copyright © 2013 PE Volodymyr Shymanskyy. All rights reserved.
# Public Domain code
use strict;
use warnings;
use List::Util qw(shuffle);
sub get_ext_ip {
my @serv = qw(
myip.dnsomatic.com
ipecho.net/plain
ipv4.icanhazip.com
bot.whatismyipaddress.com
www.myip.ru
);
my %addr;
foreach (shuffle @serv) {
return $1
if `wget -qO - $_` =~ /(\d+\.\d+.\d+.\d+)/
and ++$addr{$1} >= 2;
}
return '';
}
print get_ext_ip(), "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment