Created
May 22, 2013 11:15
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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