PERL - WAN-IP des Routers via UPnP abrufen
Beschreibung:
-
Ruft mittels des UPnP-Standarts die WAN-IP des IGD ab.
Wird in der Konsole ausgeführt und gibt die IP-Adresse in plain/text zurück.
Ist für den Einsatz als CGI konzipiert.
-
#! /usr/bin/perl -w
use HTTP::Request;
use HTTP::Headers;
use LWP::UserAgent;
use XML::Simple;
use POSIX qw(strftime);
$IGDADDR = "192.168.0.1";
$iscgi = 0;
$now_string = strftime "%a, %d %b %Y %H:%M:%S %z", localtime;
if ($iscgi) {
print "Date: ".$now_string."\r\nContent-Type: text/plain\r\nConnection: Keep-Alive\r\nCache_Control: no-cache\r\nPragma: no-cache\r\n\r\n";
}
my $uri = "http://" . $IGDADDR . ":49000/upnp/control/WANIPConn1";
my $data = "\n<?xml version=\"1.0\"?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n<SOAP-ENV:Body><m:GetExternalIPAddress xmlns:m=\"urn:schemas-upnp-org:service:WANIPConnection:1\"/>\n</SOAP-ENV:Body>\n</SOAP-ENV:Envelope>\n";
$h = HTTP::Headers->new(
Content_Type => "text/xml; charset=utf-8",
SOAPAction => "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress",
User_Agent => "Mozilla/4.0 (compatible; UPnP/1.0; Windows 9x)",
Host => $IGDADDR . ":49000",
Connection => "Keep-Alive",
Cache_Control => "no-cache",
Pragma => "no-cache"
);
$s = HTTP::Request->new(POST => $uri, $h, $data);
$ua = LWP::UserAgent->new;
$response = $ua->request($s);
if ($response->is_success)
{
print XMLin($response->content)->{'s:Body'}->{'u:GetExternalIPAddressResponse'}->{'NewExternalIPAddress'};
}
else
{
print STDERR $response->status_line, "\n";
}