最新消息:

PHP通过CURL自动登录TP-Link路由器进行自动重启

PHP AnyTopOne 118浏览 0评论

PHP通过CURL自动登录TP-Link路由器进行自动重启

1、首先需要用火狐浏览器FireBug调试工具仔细分析TP-Link路由器的登录URL、重启URL及各种操作需提交的数据格式

2、php程序代码如下

#!/usr/bin/php

<?php

include ‘./phpjson_class.php’;

//login_post与get_content这两个函数可以合并成一个,当时为了省事没有合并,大家可以自己合并下

function login_post($url, $cookie, $post) {

global $url_login_referer;

global $url_login_post;

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url_login_post);

curl_setopt($curl, CURLOPT_REFERER,$url_login_referer);

curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));

curl_exec($curl);

$rs = curl_multi_getcontent($curl);

curl_close($curl);

return $rs;

}

function get_content($url, $cookie,$post) {

global $url_reboot_referer;

global $url_reboot;

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url_reboot);

curl_setopt($curl, CURLOPT_REFERER,$url_reboot_referer);

curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));

curl_exec($curl);

$rs = curl_multi_getcontent($curl);

curl_close($curl);

return $rs;

}

//用火狐浏览器FireBug分析TP-Link路由器系统管理后台登录URL需提交的JSON数据信息

$post = array (

‘data’ => ‘{“method”:”login”,”params”:{“username”:”super”,”password”:”382b9db03e3a50f9079355713b1ff8d1db09607d6f59aa2108def3cd5dd825e9401503ab12c6604d3f8d33ccfc312542a05fe36f0996d30355694baaaed0b43cca19260aea139540bf5a4ad08131a73d59ae861f6b92c5c7fe3e9fcfc26d2c9d7b97e4188169bbfbc54b36af9dd6a1a1f3077bf376fab524cbb79a462c7a9726c2c3c”}}’,

);

//TP-Link路由器登录URL

$url_base =’http://192.168.1.1:8079′;

//TP-Link路由器登录URL的前URL

$url_login_referer = $url_base . ‘/webpages/login.html’;

$url_login_post = $url_base . ‘/cgi-bin/luci/;stok=/login?form=login’;

//TP-Link路由器重启URL的前URL

$url_reboot_referer = $url_base . ‘/webpages/index.html’;

$cookie = dirname(__FILE__) . ‘/cookie.txt’;

$result = login_post($url_login_post, $cookie, $post);

$phpjson = new PHPJSON( 16 ) ;

$arr = $phpjson->decode($result);

$stok = $arr[‘result’][‘stok’];

$url_reboot = $url_base . “/cgi-bin/luci/;stok=$stok/admin/firmware?form=config”;

//TP-Link路由器重启需提交的JSON数据

$post = array (

‘data’ => ‘{“method”:”reboot”}’,

);

$content = get_content($url_reboot, $cookie,$post);

@unlink($cookie);

?>

3、最后cron计划任务中配置好定时重启即可

转载请注明:PHP学习 » PHP通过CURL自动登录TP-Link路由器进行自动重启

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址