我们经常会碰到需要自动换ip的需求,比方模拟点击投票,数据采集被封ip,alexa作弊等等,也就是需要经常换ip的,我们都可以通过php控制路由器来换ip,这样就不需要用按键精灵搞得那么累了。呵呵。
有的路由器是提供通过web方式管理路由器设置的,中间有一个功能就是断线和重新连接,我们就利用这个功能,呵呵,下面看代码:
function router($target=''){
$server = '192.168.1.1'; // ip address
$host = '192.168.1.1'; // domain name
$port = 80;
$referer = 'http://'.$host.$target; // referer
$username = admin;#adsl的路由的用户名
$password = admin;#adsl的路由的管理密码
$authorization = base64_encode($username.:.$password); //echo $authorization; exit();
$file = fsockopen($server, $port, $errno, $errstr, 30);
if ($file) {
$out = get $target http/1.1\r\n;
$out .= host: $host\r\n;
$out .= referer: $referer\r\n;
$out .= authorization: basic $authorization\r\n;
$out .= connection: closern\r\n;
fputs($file, $out);
$makefile = $buffer = ;
while ($buffer = fread($file,4096)){
$makefile = $makefile.$buffer;
}
fclose($file);
}
}
router(/userrpm/statusrpm.htm?disconnect=%b6%cf%20%cf%df&wan=1);
//下面附上php获取当前网关ip地址
function getip(){
$target=/userrpm/statusrpm.htm;
$server = '192.168.1.1'; // ip address
$host = '192.168.1.1'; // domain name
$port = 80;
$referer = 'http://'.$host.$target; // referer
$username = admin;#adsl的路由的用户名
$password = admin;#adsl的路由的管理密码
$authorization = base64_encode($username.:.$password); //echo $authorization; exit();
$file = fsockopen($server, $port, $errno, $errstr, 30);
if ($file) {
$out = get $target http/1.1\r\n;
$out .= host: $host\r\n;
$out .= referer: $referer\r\n;
$out .= authorization: basic $authorization\r\n;
$out .= connection: closern\r\n;
fputs($file, $out);
$makefile = $buffer = ;
while ($buffer = fread($file,4096)){
$makefile = $makefile.$buffer;
}
fclose($file);
}
//匹配当前网关
$pattern=/\[a-z0-9]{2}\-[a-z0-9]{2}\-[a-z0-9]{2}\-[a-z0-9]{2}\-[a-z0-9]{2}\-[a-z0-9]{2}\, \([0-9\.]+)\/s;
if(preg_match_all($pattern,$makefile,$match)){
$nowip=$match[1][1];
}
return $nowip;
}
