So, I thought the ideal solution would be to just get the IP direct from the gateway itself! Now, I've reflashed my gateway to the stock echolife firmware so I have access to the web interface panel which this needs! Also note that it does some pretty lame output with the headers too. Anyway, this WILL need to be modified to work with your gateway I think, it works for mine with the specified WAN interface name!
Anyway, this in in PHP and uses CURL (I dislike CURL immensly but for some reason it wasn't liking my fsockopen and kept returning a piece of javascript code)
<?PHP
/*
Echolife WAN IP Retriever
Version 0.5
Created by n_K, 2012
Modify and use this script how you please.
*/
function DoCurl($URL, $POST = null, $Cookies = null)
{
//Get a page using CURL.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 6);
curl_setopt($curl, CURLOPT_USERAGENT, 'EcholifeIPUpdater/0.5 (n_K)');
curl_setopt($curl, CURLOPT_HEADER, true);
if (!is_null($POST))
{
curl_setopt($curl, CURLOPT_POST, true);
if (strlen($POST) > 2)
{
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST);
}
}
else
{
curl_setopt($curl, CURLOPT_POST, false);
}
if (!is_null($Cookies))
{
curl_setopt($curl, CURLOPT_COOKIE, $Cookies);
}
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
function CurlCookies($Data)
{
//Extract cookies from received data. (MODIFIED FOR USE WITH ECHOLIFE ONLY!)
$Matches = preg_split('/Set-cookie:(.*?);/', $Data, -1, PREG_SPLIT_DELIM_CAPTURE);
$i = 0;
$Cookies = '';
while ($i < count($Matches))
{
if ($i%2 == 1)
{
//Odd number-index!
$Cookies .= $Matches[$i].'; ';
}
++$i;
}
$Cookies = substr($Cookies, 0, strlen($Cookies)-1);
return $Cookies;
}
//Login to the modem as unpriviledged user and get the cookies
$Login = DoCurl('http://192.168.1.1/html/login.cgi?Username='./* PUT YOUR ECHOLIFE GATEWAY LOGIN USERNAME HERE */.'&Password='.rawurlencode(base64_encode(/* PUT YOUR ECHOLIFE GATEWAY PASSWORD HERE */)).'&Language=0&RequestFile=html/status/internetstatus.gz.html', 'a', 'Cookie=LoginTimes=0:LoginOverTime=0; FirstMenu=Admin_0; SecondMenu=Admin_0_0; ThirdMenu=Admin_0_0_0; sessionID=2222224564789011; Language=English');
$Cookies = CurlCookies($Login);
//Get the internet status page
$BigData = explode("\n", DoCurl('http://192.168.1.1/html/status/internetstatus.asp', null, $Cookies));
if (count($BigData) > 3)
{
//Looks like we have some data to parse!
foreach ($BigData as $ThisLine)
{
/* CHANGE YOUR GATEWAY INTERFACE BELOW TO WHAT IS DISPLAYED ON WAN PAGE 'WanPPP2' IN MY CASE */
if (substr($ThisLine, 0, 14) == 'var WanPPP2 = ')
{
//We have it!
$Data = $ThisLine;
break;
}
}
if (strlen($Data) > 5)
{
//Got it!
/* CHANGE YOUR GATEWAY INTERFACE BELOW TO WHAT IS DISPLAYED ON WAN PAGE 'ptm1.101' IN MY CASE */
$Data = explode(',', explode('ptm1.101",', $Data)[1]);
/*
0 = connected/not
1 = IP
2 = DNS servers
3 = ?
4 = ?
*/
if ($Data[0] == '"Connected"')
{
//Yay! We've got the IP!
$IP = substr($Data[1], 1, (strlen($Data[1])-2));
if (preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $IP, $BB) > 0)
{
//IP is in right format too!
/* PUT YOUR IP UPDATING/USING CODE HERE! */
}
else
{
//No valid IP found
}
}
else
{
//Error, no connection!
}
}
else
{
//Couldn't find WAN interface
}
}
else
{
//No data returned
}
?>
So there you go, maybe it'll be of use to someone!







