Daten|teiler
Kopieren als Kulturtechnik

[Update] Get your IP address with Perl

6. April 2006 von Christian Imhorst

To get your IP address with a web browser is simple, because you only have to surf to whatismyip.com or look at the bottom of this page. For those who wonder how they can get their IP address behind a router from a script, here is my solution in perl:

#!/usr/bin/perl -w
use strict;
use LWP::Simple;
 
my $url = ("http://www.whatismyip.com/automation/n09230945.asp");
my $var = get($url);
 
$var =~ m/(\d+).(\d+).(\d+).(\d+)/;
print "Your IP address is: $1.$2.$3.$4 \n";

I got the idea for the script from cray at debianer.org (only in german). He has solved the problem in the command line. Here is the solution in two lines because the URL is very long:

URL="http://www.whatismyip.com/automation/n09230945.asp"
lynx -dump $URL | grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9] \\
[0-9]*[.][0-9][0-9]' | awk '{print $1}'

Don’t forget: For this you need lynx, grep and awk. Then you have to write the text above in one single line.

[Update]: But it is also possible to realise this with perl in just one single line using the command line:

perl -MLWP::Simple -le 'print get("http://www.whatismyip.com/
automation/n09230945.asp")=~/(\d+.)(\d+.)(\d+.)(\d+)/i'

This looks a little bit more like perl, does it not? You do not have the perl module LWP::Simple? No problem, you can install it with:

sudo perl -MCPAN -e 'install  LWP::Simple'

Geschrieben in English, Programmieren