perl检测网站首页状态

perl检测网站首页状态代码,随便写的,一起交流

 
 
  1. #!/usr/bin/perl -w 
  2.  
  3. use strict; 
  4. use LWP::UserAgent; 
  5. use Net::SMTP; 
  6. use POSIX qw/strftime/; 
  7.  
  8. print <<EOF; 
  9. +--------------------------------------+ 
  10. +----made by Henry He on 2011/03/03----+ 
  11. +--------------------------------------+ 
  12. EOF 
  13.  
  14. my @array = (); 
  15. my $count = 0; 
  16.  
  17. open FILE,'<','url.txt' or die "$!\n"
  18.  
  19. while (<FILE>) { 
  20.        chomp; 
  21.        /(\S+)/; 
  22.        push @array,$1
  23.  
  24. close FILE; 
  25.  
  26. foreach my $url (@array) { 
  27.  
  28.         $count++; 
  29.  
  30.         my $obj = LWP::UserAgent->new
  31.                                       keep_alive=>1, 
  32.                                       timeout=>60); 
  33.  
  34.         my $req = HTTP::Request->new(GET=>"$url"); 
  35.  
  36.         my $res = $obj->request($req); 
  37.  
  38.         if ($res->is_success) { 
  39.             print $url,"\t",$res->status_line,"\n"
  40.         } else { 
  41.             print "$url\tFailed:\t","\n"
  42.             sendemail("$url\tFailed:\t" . $res->status_line,"\n"); 
  43.         } 
  44.  
  45.  
  46. my $cur_time = strftime "%Y-%m-%d",localtime; 
  47.  
  48. print "\n"
  49. print <<EOF; 
  50. +-------------------------------------+ 
  51. +      Finished check $count urls     + 
  52. +-------------------------------------+ 
  53. EOF 
  54.  
  55. printlog("finished check $count urls at $cur_time...\n"); 
  56.  
  57. sub sendemail { 
  58.     my $content = shift; 
  59.     #change it for your smtp server 
  60.     my $host = 'smtp.163.com'
  61.     #your email account here 
  62.     my $mail_from = 'your_email_account'
  63.     #change it what you want to send here 
  64.     my $mail_to = 'abc@abc.com'
  65.     #email subject 
  66.     my $subject = 'Error Notice'
  67.  
  68.     my $smtp = Net::SMTP->new($host
  69.                               Hello=>'localhost'
  70.                               Timeout=>120, 
  71.                               Debug=>1 
  72.                               ); 
  73.      # modify it with your email username and password 
  74.      $smtp->auth('your_email_username','your_email_password'); 
  75.      $smtp->mail($mail_from); 
  76.      $smtp->to($mail_to); 
  77.      $smtp->data(); 
  78.      $smtp->datasend("To: $mail_to\n"); 
  79.      $smtp->datasend("From: $mail_from\n"); 
  80.      $smtp->datasend("Subject: $subject\n"); 
  81.      $smtp->datasend("\n"); 
  82.      $smtp->datasend("$content\n\n"); 
  83.      $smtp->dataend(); 
  84.      $smtp->quit; 
  85.  
  86.  
  87. sub printlog { 
  88.     my $str = shift; 
  89.     open LOG,'>>','check_url_status.log' or die "$!\n"
  90.     print LOG "$str"
  91.     close LOG; 

url.txt

code:

 
 
  1. http://www.abc.com 
  2. http://www.test.com 
  3. http://www.xyz.com 
  4. ………………………… 
  5. ………………………… 
  6. ………………………… 
  7. http://www.def.com 
  8. http://www.123.com

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...