Created
January 22, 2014 22:40
-
-
Save vibbow/8568866 to your computer and use it in GitHub Desktop.
从TP-Link管理页面抓取外网IP的脚本
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
<?php | |
// TP-Link 外网IP获取脚本 | |
// 用于旧版普通路由器 | |
// 测试通过:TL-WR840N | |
function tp_link_old($ip = '192.168.1.1', $username = 'admin', $password = 'admin') { | |
$address = "http://{$ip}/userRpm/StatusRpm.htm"; | |
$userpwd = "{$username}:{$password}"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $address); | |
curl_setopt($ch, CURLOPT_USERPWD, $userpwd); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$keyword = 'var wanPara = new Array('; | |
$start_pos = strpos($result, $keyword) + strlen($keyword); | |
$end_pos = strpos($result, ');', $start_pos); | |
$content_length = $end_pos - $start_pos; | |
$content = substr($result, $start_pos, $content_length); | |
$content_array = explode(',', $content); | |
$content_array = str_replace('"', '', $content_array); | |
foreach ($content_array as $key => $value) | |
$content_array[$key] = trim($value); | |
$ip = $content_array[2]; | |
if ($ip == '0.0.0.0') | |
return FALSE; | |
else if (!ip2long($ip)) | |
return FALSE; | |
else | |
return $ip; | |
} | |
// 用于新版普通路由器 | |
// 测试通过:TD-W89741N, TD-W89841N | |
function tp_link_new($ip = '192.168.1.1', $username = 'admin', $password = 'admin') { | |
$address = "http://{$ip}/cgi?5"; | |
$userpwd = "{$username}:{$password}"; | |
$post = "[WAN_PPP_CONN#0,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $address); | |
curl_setopt($ch, CURLOPT_USERPWD, $userpwd); | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$keyword = 'externalIPAddress='; | |
$start_pos = strpos($result, $keyword) + strlen($keyword); | |
$end_pos = strpos($result, "\n", $start_pos); | |
$content_length = $end_pos - $start_pos; | |
$ip = trim(substr($result, $start_pos, $content_length)); | |
if ($ip == '0.0.0.0') | |
return FALSE; | |
else if (!ip2long($ip)) | |
return FALSE; | |
else | |
return $ip; | |
} | |
// 用于企业多线路由器 | |
// 测试通过:TL-R478+ | |
function tp_link_dual_wan($ip = '192.168.1.1', $username = 'admin', $password = 'admin') { | |
$base_url = "http://{$ip}/"; | |
// 登录前需要先获取cookies | |
tplink_remote_access($base_url); | |
// 登录 (需要从cookies文件里获取到nonce,并计算出加密后的encoded) | |
$cookies_file = file('cookies.txt'); | |
$cookies_content = $cookies_file[4]; | |
$cookies_elements = explode("\t", $cookies_content); | |
$nonce = trim($cookies_elements[count($cookies_elements) - 1]); | |
$encoded = "{$username}:" . md5("{$username}:{$password}:{$nonce}"); | |
$post = 'encoded=' . urlencode($encoded) . '&nonce=' . urlencode($nonce) . '&URL=' . urlencode('../logon/loginJump.htm'); | |
tplink_remote_access("{$base_url}logon/loginJump.htm", "{$base_url}logon/logon.htm", $post); | |
// 如果有其他用户在登录路由器,这里需要强行登录 | |
tplink_remote_access("{$base_url}logon/loginConfirm.htm", "{$base_url}logon/loginJump.htm"); | |
// 抓取系统信息页面 | |
$information = tplink_remote_access("{$base_url}userRpm/Monitor_sysinfo_wanstatus.htm", "{$base_url}userRpm/Monitor_sysinfo.htm"); | |
// 从系统信息页面中取得四个WAN口的IP | |
$keyword = 'var wanInfoArr = new Array('; | |
$start_pos = strpos($information, $keyword) + strlen($keyword); | |
$end_pos = strpos($information, ');', $start_pos); | |
$content_length = $end_pos - $start_pos; | |
$content = substr($information, $start_pos, $content_length); | |
$content_array = explode(',', $content); | |
$content_array = str_replace('"', '', $content_array); //清理掉IP信息左右两侧的双引号 | |
foreach ($content_array as $key => $value) | |
$content_array[$key] = trim($value); //清理掉IP信息左右两侧的换行符 | |
$wan_ip = array($content_array[3], $content_array[14], $content_array[25], $content_array[36]); | |
// 退出登录 | |
tplink_remote_access("{$base_url}logon/logout.htm", "{$base_url}userRpm/Index.htm"); | |
//删除临时cookies文件 | |
unlink('cookies.txt'); | |
return $wan_ip; | |
} | |
function tplink_remote_access($url, $referer = FALSE, $post = FALSE) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
if ($referer !== FALSE) { | |
curl_setopt($ch, CURLOPT_REFERER, $referer); | |
} | |
if ($post !== FALSE) { | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); | |
} | |
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)"); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment