2个小时拿下Perl语言:自动监控服务器内网站访问ip次数并预警(附源码)

[作者]
网名: 猪头三
Email: pliceman_110@163.com
QQ: 643439947
编程生涯: 2001~至今[2014年]
职业生涯: 11年
开发语言: C/C++; x86asm; Object Pascal; Object-C; C#;
开发工具: VC++; Delphi;
研发领域: Windows应用软件安全; Windows系统内核安全; Windows系统磁盘数据安全;
技能种类: 逆向 驱动 磁盘 文件
[序言]
一直都不是Linux服务器专业人员. 但恰恰自己又拥有2台国外服务器, 上面有好多关于我国外软件的产品网站. 这2台服务器每天都遭受各种第3世界国家攻击以及主流国家恶意爬虫和wp_login.php攻击. 每次维护一次访问日记都需要1个多小时, 很麻烦. 再一个Linux命令苦涩难懂, 勉强会用几个, 但还是不顶用. 就在前几天心烦了, 干脆自己写脚本解析日记算了. 就这个念头一闪, 就诞生了这个perl脚本出来.
[学习]
perl语言认真看2个小时, 然后再mac os x系统装上eclips+perl插件就开工了(备注: 不会用vi, 因为人笨), 断断续续用了2天时间终于完成了第一个版本, 大约700多行. 在开发过程中, 其实很简单: 就是把C语言的逻辑思维转换成perl代码. 为什么要这样做, 因为脚本语言通病就是语法糖过多, 会让你迷失在语法糖里面而无法写出正常的程序, 不是每个开发人员有拥有那种开发语言的天赋(备注:我个人认为只有这样的天赋才能接受各种奇怪的语法糖). 也就是一直坚持这个原则才让这个perl脚本诞生.
[差异]
perl和python都很好上手, 只要你有C语言基础, 玩这个很简单. 比JS简单多了. 那我为什么用perl呢?其实是我的服务器默认没有安装python, 倒反默认安装了ruby, 比较神奇. 本来想用ruby的, 因为我第一个脚本语言就是ruby, 但是后来想想linux服务器一般大头都是perl语言, 因此选择了perl而不是ruby.
[源码]
#!usr/bin/perl
#access-log
#     ip访email
#     
# http://www.x86asm.com
#: v0.0.1 
#: 2014-07-15
#Linux,
#     .
#使linuxcrontab -e
#
BEGIN  {
   push  @ INC , "/Users/PigHeadThree/perl5/lib/perl5"  ;
}
#use 5.010;
use   strict  ;
use   autodie ;
use   Try :: Tiny ;
use   Tie :: File ;
use   Fcntl   'O_RDONLY'  ;
use   File :: Spec :: Functions   qw ( rel2abs ) ;
use   File :: Basename   qw ( dirname   basename ) ;
use   File :: Spec ;
use   File :: Copy   qw ( copy );
use   MIME :: Lite  ;
use   MIME :: Base64 ;
use   Authen :: SASL :email
#===================================
#===================================
#()()
my   $g_str_App_Flag   =   'pht_monitor'  ;
#access-logsgooglebing()
#google bingipip
my   $g_str_grep_Bot   =   'googlebot|bingbot'  ;
#ip访()
my   $g_int_Limit_VisitIp   =   900  ;
#()
my   $g_str_ServerName   =   "PHT Server"  ;
#email()
my   $g_str_email_From   =   'xxxxxxx@163.com'  ;
my   $g_str_email_Pass   =   'xxxxxxx'          ;
my   $g_str_eamil_Smtp   =   'smtp.163.com'     ;
my   $g_str_eamil_To     =   'xxxxxxx@qq.com'   ;
#===================================
try
{
     & fun_RunningLog ( "main() start..." ) ;
     & main () ;
     & fun_RunningLog ( "main() end." ) ;
}
catch
{
     & fun_RunningLog ( "===========main() running error===========" ) ;     
};
sub   main ()
{
     # www.x86asm.com
     #
     #: x86asm
     #x86asm.com,
     #/home/x86asm/access-logs/x86asm.com
     #ip访
     my   $str_Site_URL           =   "www.x86asm.com"  ;
     my   $str_Site_Name          =   "x86asm_"  ;
     my   $str_Site_LogFilePath   =   "/home/x86asm/access-logs/x86asm.com"  ;
     fun_CountTargetSite ( $str_Site_URL ,
                         $str_Site_Name ,
                         $str_Site_LogFilePath ) ;
                              
} #End main()
                  
#=======================================
#ip访
#param_1 : 
#param_2 : 
#param_3 : access_log 
sub   fun_CountTargetSite ()
{
    
     #0
     if  (@ _   !=   3 )
    {
          return   0  ;
    }
    
    ( my   $str_param_Site_URL ,
      my   $str_param_Site_Name ,
      my   $str_param_Site_LogFilePath =  @ _  ;
    
     my   % hash_VisitIp_Count  ;
    
     & fun_RunningLog ( "[$str_param_Site_URL] : start..." );
    
     & fun_GetVisitIp ( $str_param_Site_LogFilePath ,
                         $g_int_Limit_VisitIp ,
                         & fun_GetLocaltime ( "/" ) ,
                         \% hash_VisitIp_Count ) ;
     if  ( & fun_IsNeedUpdate ( $str_param_Site_Name .& fun_GetLocaltime ( "_" ) . ".txt" ,
                                \% hash_VisitIp_Count eq   1 )
    {
        
         #
         & fun_VisitIpSaveToCurrentPathFile ( $str_param_Site_Name .& fun_GetLocaltime ( "_" ) . ".txt" ,
                                                  \% hash_VisitIp_Count );
          #EMAIL
          my   $str_SendEmail_Data   =   & fun_GetSendEmailToData ( $str_param_Site_Name .& fun_GetLocaltime ( "_" ) . ".txt" ) ;
          if  ( $str_SendEmail_Data   ne   0 )
         {
              & fun_AutoSendEmailToAlarm ( $g_str_ServerName . "[$str_param_Site_URL]" . "Alarm Ip Visit" ,
                                        $str_SendEmail_Data ) ;
         }
         
    }
     else
    {
        
    }    
     & fun_RunningLog ( "[$str_param_Site_URL] : end." );
    
} # End fun_CountTargetSite() 
#
#param_1 : 
sub   fun_GetLocaltime ()
{
    
     #0
     if  (@ _   !=   1 )
    {
          return   0  ;
    }
    
     #
     my   % hash_montoname   =  (
          "01"   =>   "Jan" ,
          "02"   =>   "Feb" ,
          "03"   =>   "Mar" ,
          "04"   =>   "Apr" ,
          "05"   =>   "May" ,
          "06"   =>   "Jun" ,
          "07"   =>   "Jul" ,
          "08"   =>   "Aug" ,
          "09"   =>   "Sep" ,
          "10"   =>   "Oct" ,
          "11"   =>   "Nov" ,
          "12"   =>   "Dec" ) ;
         
     #
     my  ( $sec , $min , $hour , $mday , $mon , $year_off , $wday , $yday , $isdat =   localtime ;
         ( $mday ,   $mon ,   $year_off =  (
          sprintf ( "%02d" ,   $mday ) ,
          sprintf ( "%02d" ,   $mon +1 ) ,
          $year_off +1900 );
     #
     $mon   =   $hash_montoname { $mon } ;
     return   $mday . $_ [ 0 ] . $mon . $_ [ 0 ] . $year_off ;
              
} # End fun_GetLocaltime()
#
sub   fun_GetLocaltimeToLog ()
{
     #
     if  (@ _ )
    {
          return   0  ;
    }
    
     #
     my  @ array_montoname   =   qw ( Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec ) ;
         
     #
     my  ( $sec , $min , $hour , $mday , $mon , $year_off , $wday , $yday , $isdat =   localtime ;
         ( $sec ,   $min ,   $hour ,   $mday ,   $year_off =  (
          sprintf ( "%02d" ,   $sec ) ,
          sprintf ( "%02d" ,   $min ) ,
          sprintf ( "%02d" ,   $hour ) ,
          sprintf ( "%02d" ,   $mday ) ,
          $year_off +1900 );
     #
     $mon   =   $array_montoname [ $mon ] ;
     return   $mday . '/' . $mon . '/' . $year_off . '  ' . $hour . ':' . $min . ':' . $sec  ;  
    
} # End fun_GetLocaltimeToLog()
#
#param_1 : 
sub   fun_RunningLog ()
{
     #0
     if  (@ _   !=   1 )
    {
          return   0  ;
    }
    
    ( my   $str_param_Print =  @ _  ;
    
     #
     my   $str_LogTime   =   '[' .& fun_GetLocaltimeToLog () . ']'  ;
    
     #
     my   $str_Log_FileName   =   'run_' .& fun_GetLocaltime ( '_' ) . '.txt'  ;
     $str_Log_FileName      =   File :: Spec -> join ( & fun_GetCurrentAppPath () ,
                                             $str_Log_FileName ) ;
                                            
     unless ( - e   $str_Log_FileName )
    {
          unless ( open ( HANDLE_OPEN ,   ">$str_Log_FileName" ))
         {
               return   0  ;
         }
    }
     else
    {
          unless ( open ( HANDLE_OPEN ,   ">>$str_Log_FileName" ))
         {
               return   0  ;
         }
    }
    
     #
     print   HANDLE_OPEN   $str_LogTime . ' : ' . $str_param_Print . "\n"  ;
    
     close ( HANDLE_OPEN ) ;
    
     return   1  ;
         
} #End fun_RunningLog()
#
sub   fun_GetCurrentAppPath ()
{
     #
     if  (@ _ )
    {
          return   0  ;
    }
    
     return   dirname ( rel2abs ( __FILE__ )) ;
    
} # End fun_GetCurrentAppPath()
#
#param_1 : 
sub   fun_CopyFileToCurrentAppPath ()
{
     my   $str_CopyFileToPath   =   ""  ;
     my   $str_CopyFileName     =   ""  ;
     my   $str_CurrentAppPath   =   ""  ;
    
     #0
     if  (@ _   !=   1 )
    {
          return   0  ;
    }
     else
    {
          my   $str_param_CopyFilePath   =   $_ [ 0 ] ;
          # -e   -d 
          if  ( - e   $str_param_CopyFilePath )
         {
               #()
               $str_CurrentAppPath   =   & fun_GetCurrentAppPath () ;
               $str_CopyFileName     =   basename ( $str_param_CopyFilePath ) ;
               $str_CopyFileToPath   =   File :: Spec -> join ( $str_CurrentAppPath ,   $str_CopyFileName ) ;
               if  ( - e   $str_CopyFileToPath )
              {
                    #
                    if  ( $str_CopyFileToPath   =~   /$g_str_App_Flag/i )
                   {
                         & fun_RunningLog ( "fun_CopyFileToCurrentAppPath() deleteing $str_CopyFileToPath..." ) ;
                         unlink ( $str_CopyFileToPath );
                         & fun_RunningLog ( "fun_CopyFileToCurrentAppPath() deleted $str_CopyFileToPath." ) ;    
                   }
              }
               copy ( $str_param_CopyFilePath ,   $str_CopyFileToPath ) ;
              
               return   $str_CopyFileToPath  ;
              
         }
    }
    
     return   0  ;
    
} # End fun_CopyFileToCurrentAppPath()
#ip
sub   fun_GetIpByStr ()
{
     #0
     if  (@ _   !=   1 )
    {
          return   0  ;
    }
    
    ( my   $str_param_Target =  @ _  ;
    
     if  ( $str_param_Target   =~   /(\d+\.\d+\.\d+\.\d+)/ )
    {
          return   "$1"  ;  #IP
    }
    
     return   0  ;
    
} # End fun_GetIpByStr()
#ip访
#param_1 : 访
#param_2 : IP访
#param_3 : 访 (02/jul/2014)
#param_4 :  
sub   fun_GetVisitIp ()
{
     #0
     if  (@ _   !=   4 )
    {
          return   0  ;
    }
     #
    ( my   $str_param_VisitIp_LogFilePath ,
      my   $int_param_VisitIp_Count ,
      my   $time_param_Get ,
      my   $hash_param_VisitIp =  @ _  ;
     
     #
     if  ( $time_param_Get   eq   "NO" )
    {
          $time_param_Get   =   & fun_GetLocaltime ( "/" ) ;
    }
     
     #
     if  ( - e   $str_param_VisitIp_LogFilePath )
    {
          my   $str_VisitIp_CopyToPath   =   & fun_CopyFileToCurrentAppPath ( $str_param_VisitIp_LogFilePath ) ;
          if  ( $str_VisitIp_CopyToPath   eq   0 )
         {
               #
               return   0  ;
         }
    
          #
          my  @ array_VisitIp_Load  ;
          my   $int_VisitIp_FileLineNums  ;
          if  ( - e   $str_VisitIp_CopyToPath )
         {
               tie  @ array_VisitIp_Load ,
                   'Tie::File' ,
                   $str_VisitIp_CopyToPath ,
                   mode   =>   O_RDONLY  ;
                  
               # 
               $int_VisitIp_FileLineNums   =  @ array_VisitIp_Load   -   1  ;
              
               #
               my   $str_Temp_Line            =   ""  ;
               my   $str_Temp_VisitIp         =   ""  ;
               my   $str_Temp_VisitIp_Count   =   0   ;
               my   % hash_Temp_VisitIp        =  () ;
               while  ( $int_VisitIp_FileLineNums   >=   0 )
              {
                    $str_Temp_Line   =   $array_VisitIp_Load [ $int_VisitIp_FileLineNums ] ;
                    if  ( $str_Temp_Line   =~   /$time_param_Get/i )
                   {
                         if  ( $str_Temp_Line   =~   /$g_str_grep_Bot/i )
                       {
                           
                       }
                        else
                       {
              
                            #ip
                            $str_Temp_VisitIp   =   & fun_GetIpByStr ( $str_Temp_Line ) ;
                            if  ( $str_Temp_VisitIp   eq   0 )
                           {
                           }
                            else
                           {
                                #ip访()
                                if  ( exists   $hash_Temp_VisitIp { $str_Temp_VisitIp })
                               {
                                    $hash_Temp_VisitIp { $str_Temp_VisitIp +=   1  ;
                                   
                                    #,
                                    if  ( $hash_Temp_VisitIp { $str_Temp_VisitIp >=   $int_param_VisitIp_Count )
                                   {
                                       
                                        if  ( exists   $$hash_param_VisitIp { $str_Temp_VisitIp })
                                         {
                                              $$hash_param_VisitIp { $str_Temp_VisitIp +=   1  ;  
                                         }
                                          else
                                         {
                                              $$hash_param_VisitIp { $str_Temp_VisitIp =   $int_param_VisitIp_Count  ;                    
                                         }
                                       
                                   }
                               }
                                else
                               {
                                    $hash_Temp_VisitIp { $str_Temp_VisitIp =   1  ;
                               }
                               
                           }
                           
                       }
                   }
                    else
                   {
                         last  ;
                   }
                   
                    $int_VisitIp_FileLineNums   -=   1  ; 
              }
              
               #
               untie  @ array_VisitIp_Load  ; 
              
               return   1  ;   
         }
    }
     else
    {
          & fun_RunningLog ( "fun_GetVisitIp() $str_param_VisitIp_LogFilePath no found." ) ;
    }
    
     return   0  ;
    
} # End fun_GetVisitIp()
#
#param_1 : 
#param_2 : 
sub   fun_VisitIpSaveToCurrentPathFile ()
{
     #0
     if  (@ _   !=   2 )
    {
          return   0  ;
    }
    
    ( my   $str_param_SaveToFileName ,
     my    $hash_param_Conent =  @ _  ;
    
     #
     if  (( length ( $str_param_SaveToFileName ==   0 or
       (( keys   % $hash_param_Conent ==   0 ))
    {
          return   0  ;
    }
    
     #
     my   $str_SaveToFilePath   =   File :: Spec -> join ( & fun_GetCurrentAppPath () ,
                              $str_param_SaveToFileName ) ;
     if  ( - e   $str_SaveToFilePath )
    {
          #
          if  ( $str_SaveToFilePath   =~   /$g_str_App_Flag/i )
         {
               & fun_RunningLog ( "fun_VisitIpSaveToCurrentPathFile() deleteing $str_SaveToFilePath..." ) ;
               unlink ( $str_SaveToFilePath );
               & fun_RunningLog ( "fun_VisitIpSaveToCurrentPathFile() deleted $str_SaveToFilePath." ) ;
         }
    }
    
     if  ( open ( HANDLE_FILE ,   ">$str_SaveToFilePath" ))
    {
          my   $hash_Keys  ;
          my   $hash_Values  ;
         
          foreach   $hash_Keys  ( sort   keys   % $hash_param_Conent )
         {
               $hash_Values   =   $$hash_param_Conent { $hash_Keys } ;
               print   HANDLE_FILE   "$hash_Keys => $hash_Values \n"  ;
         }
         
          close ( HANDLE_FILE ) ;
    }
     else
    {
          return   0  ;
    }
    
} # End fun_VisitIpSaveToCurrentPathFile()
#(.txt)
#param_1 : 
#param_2 : 
sub   fun_IsNeedUpdate ()
{
     #0
     if  (@ _   !=   2 )
    {
          return   0  ;
    }
    
    ( my   $str_param_FileName ,
     my    $hash_param_Conent =  @ _  ;
     #
     if  (( length ( $str_param_FileName ==   0 or
       ( keys   % $hash_param_Conent   ==   0 ))
    {
          return   0  ;
    }
    
     my   $bool_IsNeedUpdate   =   0  ;  #
     my   $str_Ip   =   ""  ;
     my   % hash_Load_Ip  ;
     my   $str_OpenFilePath   =   File :: Spec -> join ( & fun_GetCurrentAppPath () ,
                                             $str_param_FileName ) ;
     #ip
     if  ( - e   $str_OpenFilePath )
    {
          if  ( open  ( HANDLE_OPEN ,   $str_OpenFilePath ))
         {
               while ( my   $str_Temp_Line   =   < HANDLE_OPEN > )
              {
                    $str_Ip   =   & fun_GetIpByStr ( $str_Temp_Line ) ;
                    if  ( $str_Ip   eq   0 )
                 {
                 }
                  else
                 {
                      ip
                      $hash_Load_Ip { $str_Ip =   1  ;    
                 }
              }
              
               close ( HANDLE_OPEN );
              
               #
               my   $str_Temp_Value  ;
               while (( $str_Ip ,   $str_Temp_Value =   each   % $hash_param_Conent )
              {
                    #ip1
                 if  ( exists   $hash_Load_Ip { $str_Ip })
                {
                    
                }
                 else
                {
                     $bool_IsNeedUpdate   =   1  ;
                     last  ;
                }   
              }
         }   
    }
     else
    {
          #
          $bool_IsNeedUpdate   =   1  ;
    }
     return   $bool_IsNeedUpdate  ;
    
} # End fun_IsNeedUpdate()
#email
#param_1 : 
#param_2 : 
sub   fun_AutoSendEmailToAlarm ()
{
     #0
     if  (@ _   !=   2 )
    {
          return   0  ;
    }
    
    ( my   $str_param_Subject ,
      my   $str_param_Data =  @ _  ;
    
     if  (( length ( $str_param_Subject ==   0 or
        ( length ( $str_param_Data ==    0 )) 
     {
         return   0
     } 
     
      my   $msg_Send   =   MIME :: Lite -> new ( From      =>   $g_str_email_From ,
                                       To        =>   $g_str_eamil_To ,
                                       Subject   =>   $str_param_Subject ,
                                       Data      =>   $str_param_Data ) ;
     #$msg_Send->attr("content-type" => "text/html") ;
     $msg_Send -> send ( "smtp" ,   $g_str_eamil_Smtp ,
                     AuthUser => $g_str_email_From ,
                     AuthPass => $g_str_email_Pass ,
                     Timeout   =>   30 ) ;  # Debug => 1 
                    
      return   1  ;
    
} # End fun_AutoSendEmailToAlarm()
#email
#param_1 :  ()
sub   fun_GetSendEmailToData ()
{
     #0
     if  (@ _   !=   1 )
    {
          return   0  ;
    }
    
    ( my   $str_param_FileName =  @ _  ;
    
     if  ( length ( $str_param_FileName ==   0 )
    {
          return   0  ;
    }
    
     #
     my   $str_OpenFilePath   =   File :: Spec -> join ( & fun_GetCurrentAppPath () ,
                                             $str_param_FileName ) ;
     if  ( - e   $str_OpenFilePath )
    {
          my   $str_Content   =   ""  ;
          if  ( open  ( HANDLE_OPEN ,   $str_OpenFilePath ))
         {
               while ( my   $str_Temp_Line   =   < HANDLE_OPEN > )
              {
                    chomp ( $str_Temp_Line ) ;
                    $str_Content   .=   $str_Temp_Line   .=   "\n"   ;
              }
              
               close ( HANDLE_OPEN );
              
               if  ( length ( $str_Content !=   0 )
              {
                    return   $str_Content  ;   
              }
         }   
    }
    
     return   0  ;
    
} # End fun_GetSendEmailToData()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不是代码教父

我的创作动力离不开你的真诚激励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值