|
|
发表于 2006-8-31 22:21:02
|
显示全部楼层
LWP在蓝波宽带中的应用
作者: mrkissinger
2005-10-31
LWP是libwww的Perl接口,通过LWP可以方便的用Perl编程访问Web Server,网络中大量的spider软件就是借助LWP的强大功能。
在日常生活中,最早碰到这样的问题是在长宽,当时长宽还没有使用pppoe,而是自己做了一个Web登录,强迫用户保留一个浏览器窗口,否则很快就会超时需要重新登录。
我于是用LWP写了一个小的登录程序,用cron执行,比保留一个浏览器窗口省资源多了。
时隔数年,长宽用pppoe已经很久了,这个小程序就一直沉睡。
直到我搬家,新的住处只有蓝波,蓝波居然又采用同样的办法进行用户认证,迄今仍未改为pppoe。
于是我修改了一下,这个小程序又可以在蓝波网内工作了。
用法
在程序中找到以下内容。
<input type="password" name="passwd" value="">
<input type="hidden" name="chap" value="">
将其中的value填写成登录名和密码,用cron或windows计划任务执行即可。
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTML::Form;
use POSIX qw(strftime);
my $AuthUrl="http://192.168.252.1/";
my $html='
<form action="/fcgi/websAuth" method=" OST"">
<input type="text" name="userid" value="">
<input type="password" name="passwd" value="">
<input type="hidden" name="chap" value="">
<select name="service">
<option value="internet" selected>internet</option>
</select>
<input type="hidden" name="random" size=50 value="00007ab2000000d4">
<input type=submit name=login value="login">
</form>
';
my $alive='
<form name="alive" method="post" action="/fcgi/websAlive">
<input type="hidden" name="echoid" value="0">
<input type="hidden" name="passwd" value="b80411bab4d7b84595a889165e62118f">
<!--
<input type="hidden" name="passwd" value="a9b30e8bf3093eda553b5dee2e25b4ae">
-->
</form>
';
my $action=shift(@ARGV);
if ($action eq "refresh")
{
$html=$alive;
}
#"500 Can't connect to 192.168.130.222:80 (connect: timeout)"
open(logfile,">> /var/log/lanbo.log");
my $ua = LWP::UserAgent->new(timeout => 5);
my $form = HTML::Form->parse($html,$AuthUrl);
my $request=HTTP::Request->new;
#$ua->timeout(10);
my $response=$ua->request($form->click);
my $res=$response->content;
print "$res\n";
if ($res=~/timeout/)
{
printf logfile (strftime("%a %b %e %H:%M:%S %Y",localtime())."\tDisconnected\n");
}
if ( ($res=~/login/) || ($res=~/parent.noUnload/) )
{
print "The ************ing LANBO died again!\n";
printf logfile (strftime("%a %b %e %H:%M:%S %Y",localtime())."\n");
if ($action eq "refresh")
{
exec("/usr/local/bin/lanbo.pl");
}
}
#printf logfile (strftime("%a %b %e %H:%M:%S %Y",localtime())."\n");
close(logfile); |
|