Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5272566
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: LINUX

2009-03-18 23:55:08

在去西安的飞机上突发奇想,可以用perl写一个脚本,自动到北京市交管局网站上检查非现场违章的记录,并把结果发一封邮件出来。相信很多同学平日里访问交管局网站时都是心惊肉跳的,现在,福音来了。

而且,对于SP之流来说,这绝对是个business呢。

这个0.1版本代码不是很长,现在开放出来,欢迎有兴趣的同学继续改进,并保持同样的许可。

bjjtgl.pl

#!/usr/bin/perl -w
use LWP::UserAgent;
use Net::SMTP;
use MIME::Base64;
print "本程序仅适用于北京市机动车交通违法记录查询\n"; print "相关信息在bjjtgl.properties文件中配置\n"; print "Copyleft (C) 2006 Haisheng HU\n\n"; print "查询中......\n"; print "按 Ctrl + c 退出\n\n";
open PROPERTIES, "bjjtgl.properties" or die "不能打开配置文件\n"; $_ = join "", ; my ($use_proxy) = /\nuse_proxy\s*=\s*(\d)/; my ($http_proxy) = /\nhttp_proxy\s*=\s*([\w.:\/]+)/; my ($period) = /\nperiod\s*=\s*(\d+)/; my ($mailhost) = /\nmailhost\s*=\s*([\w.@]+)/; my ($maildomain) = /\nmaildomain\s*=\s*([\w.@]+)/; my ($mailfrom) = /\nmailfrom\s*=\s*([\w.@\ <>]+)/; my ($username) = /\nusername\s*=\s*([\w.]+)/; my ($password) = /\npassword\s*=\s*([\w.`-]+)/;
my $props = $_; my $i = 0; @subscriber = (); while (1) { if ($props =~ /\ncartype\s*=\s*([\x80-\xFF\w]+)/g) { $cartype[$i] = $1; } else { last; } if ($props =~ /\ncarid\s*=\s*(\w{6})/g) { $carid[$i] = $1; } else { last; } if ($props =~ /\ncarengine\s*=\s*(\d+)/g) { $carengine[$i] = $1; } else { last; } if ($props =~ /\nsubscriber\s*=\s*([\w.@]+)(,\s*([\w.@]+))*/g) { push @$subscriber, [$1, $3]; } else { last; } $i++; } close PROPERTIES;
while (1) { for ($i = 0; $i <= $#carid; $i++) { &check_this_car($cartype[$i], $carid[$i], $carengine[$i], $subscriber->[$i]); } sleep $period; }
sub check_this_car { my $ua = LWP::UserAgent->new; $ua->timeout(30); if ($use_proxy == 1 and $http_proxy) { $ua->proxy('http', $http_proxy); } my $response = $ua->get(''.$_[1].'&caty=02&caty_C='.$_[0].'&fdjh='.$_[2].'&sf=11'); if ($response->is_success) { $web = $response->content; } else { die $response->status_line; }
if ($web =~ /交通执法站/) { print "[$_[1]] 出现交通违法记录\n";
my $subject = "交通违法记录"; my $head = "您至少有一条交通违法记录:"; my $who = $_[1]; $web =~ /class=font121>(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2})<\/td>/g; my $when = $1; $web =~ /class=font121>([\x80-\xFF( )]+)<\/td>/g; my $where = $1; $where =~ s/( )+/ /g; $web =~ /height=500'\)">([\x80-\xFF\d%]+)<\/a>/g; my $what = $1; $what =~ s/( )+/ /g; my $sig = "--\n北京市机动车交通违法记录检查脚本友情提示";
open LOG, "bjjtgl.log" or die "不能打开日志文件\n"; my $flag = 1; while () { chomp; if (/$when/) { $flag = 0; last; } } close LOG;
if ($flag == 1) { print "[$_[1]] 发送邮件\n";
my $smtp = Net::SMTP->new($mailhost, Hello=>$maildomain, Timeout=>30, Debug=>0) or die "无法联系SMTP主机\n"; $smtp->command('AUTH LOGIN')->response(); $smtp->command(encode_base64("$username"))->response(); $smtp->command(encode_base64("$password"))->response();
for ($i = 0; $i < @{$_[3]}; $i++) { my $mailto = $_[3]->[$i]; if ($mailto && $mailto =~ /\w+@\w+(.\w+)+/) { $smtp->mail($mailfrom); $smtp->to($mailto); $smtp->data(); $smtp->datasend("From: $mailfrom\n"); $smtp->datasend("To: $mailto\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); $smtp->datasend("$head\n"); $smtp->datasend("$who\n"); $smtp->datasend("$when\n"); $smtp->datasend("$where\n"); $smtp->datasend("$what\n\n"); $smtp->datasend("$sig\n"); $smtp->dataend(); } } $smtp->quit; open LOG, ">>bjjtgl.log" or die "不能打开日志文件\n"; print LOG "[$who] $when\n"; close LOG; } } }

bjjtgl.properties

#
# 配置文件bjjtgl.properties
#
# 访问交管局网站是否使用代理 use_proxy = 0 # 代理url http_proxy = # 检查间隔(秒) period = 3600
# SMTP服务器 mailhost = smtp.victim.com maildomain = localhost.localdomain mailfrom = hacker username = hacker@victim.com password = 123456
# 第1辆 cartype = 小型汽车号牌 carid = A88888 carengine = 123456 subscriber = huhaisheng@gmail.com
# 第n辆...

bjjtgl.log
(空白文件)

待加入的功能:
1. 支持“发动机号”和“确认码”两种方式;
2. 发送邮件中包含全部违章记录;
3. 当全部违章记录发生变化时(增加或减少)发送邮件;
4. 当存在违章记录,在设定的一段时间内没有发生变化时发送邮件;
5. 发布桌面GUI版本。

阅读(1770) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~