加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 23:54:33 所属栏目:大数据 来源:网络整理
导读:[作者] 网名: 猪头三 站点:? http://www.x86asm.com Email: pliceman_110@163.com QQ:? 643439947 编程生涯: 2001~至今[2014年] 职业生涯: 11年 开发语言: C/C++; x86asm; Object Pascal; Object-C; C#; 开发工具: VC++; Delphi; 研发领域: Windows应用软件
[作者]
网名: 猪头三
站点:?http://www.x86asm.com
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 ()?;
"main()?end." )?;
catch
"===========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 ?;
"[$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 ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? % hash_VisitIp_Count );
???? ???? #EMAIL
???? ???? my ? $str_SendEmail_Data ? = ? & fun_GetSendEmailToData ( $str_param_Site_Name ".txt" )?;
if ?( $str_SendEmail_Data ? ne ? 0 )
???? ????{
???? ???????? & fun_AutoSendEmailToAlarm ( $g_str_ServerName "[$str_param_Site_URL]" "Alarm?Ip?Visit" ???? ?????????????????????????????????? $str_SendEmail_Data )?;
???? ????}
???? ????
else
????}????
"[$str_param_Site_URL]?:?end." );
#?End?fun_CountTargetSite()?
#
#param_1?:?
sub ? fun_GetLocaltime ()
#
% 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 ) ? $mon +1 ) ???? ???? $year_off +1900 );
#
???? $mon ? = ? $hash_montoname { $mon }?;
???? return ? $mday . $_ [ 0 ] . $mon . $year_off ;
???? ???? ????
#?End?fun_GetLocaltime()
#
sub ? fun_GetLocaltimeToLog ()
#
if ?(@ _ )
my ?@ array_montoname ? = ? qw ( Jan ? Feb ? Mar ? Apr ? May ? Jun ? Jul ? Aug ? Sep ? Oct ? Nov ? Dec )?;
???? ????( $sec ? $min ? $hour ? $mday ? $sec ) ? $min ) ? $hour ) = ? $array_montoname [ $mon ]?;
'/' . $year_off '??' . $hour ':' . $min . $sec ?;??
#?End?fun_GetLocaltimeToLog()
#
#param_1?:?
sub ? fun_RunningLog ()
#0
my ? $str_param_Print )? #
my ? $str_LogTime ? '[' .& fun_GetLocaltimeToLog () ']' ?;
#,?
my ? $str_Log_FileName ? 'run_' '_' ) '.txt' ?;
???? $str_Log_FileName ???? = ? File :: Spec -> join ( & fun_GetCurrentAppPath () ???????????????????????????????????????????? $str_Log_FileName )?;
????????????????????????????????????????????
unless ( - e ? $str_Log_FileName )
unless ( open ( HANDLE_OPEN ? ">$str_Log_FileName" ))
???? ???? ???? ">>$str_Log_FileName" ))
#
print ? HANDLE_OPEN ? $str_LogTime '?:?' . $str_param_Print "n" ?;
close ( HANDLE_OPEN )?;
1 ?;
#End?fun_RunningLog()
#
sub ? fun_GetCurrentAppPath ()
return ? dirname ( rel2abs ( __FILE__ ))?;
#?End?fun_GetCurrentAppPath()
#
#param_1?:?
sub ? fun_CopyFileToCurrentAppPath ()
my ? $str_CopyFileToPath ? "" ?;
my ? $str_CopyFileName ??? my ? $str_CurrentAppPath ? my ? $str_param_CopyFilePath ? = ? $_ [ 0 ]?;
#?-e???-d?
- e ? $str_param_CopyFilePath )
???? ???? ???? #()
???? ???? ???? $str_CurrentAppPath ? & fun_GetCurrentAppPath ()?;
???? ???? ???? $str_CopyFileName ??? = ? basename ( $str_param_CopyFilePath )?;
???? ???? ???? $str_CopyFileToPath ? join ( $str_CurrentAppPath ? $str_CopyFileName )?;
???? ???? ???? - e ? $str_CopyFileToPath )
???? ???? ????{
???? ???? ???? ???? #
???? ???? ???? ???? if ?( $str_CopyFileToPath ? =~ ? /$g_str_App_Flag/i )
???? ???? ???? ????{
"fun_CopyFileToCurrentAppPath()?deleteing?$str_CopyFileToPath..." )?;
???? ???? ???? ???? ???? unlink ( $str_CopyFileToPath );
"fun_CopyFileToCurrentAppPath()?deleted?$str_CopyFileToPath." )?;????
???? ???? ???? ????}
???? ???? ????}
???? ???? ???? copy ( $str_param_CopyFilePath ? $str_CopyFileToPath )?;
return ? $str_CopyFileToPath ?;
#?End?fun_CopyFileToCurrentAppPath()
#ip
sub ? fun_GetIpByStr ()
my ? $str_param_Target )? if ?( $str_param_Target ? /(d+.d+.d+.d+)/ )
return ? "$1" ?;? #IP
#?End?fun_GetIpByStr()
#ip访
#param_1?:?访
#param_2?:?IP访
#param_3?:?访?(02/jul/2014)
#param_4?:??
sub ? fun_GetVisitIp ()
4 )
#
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 ? "/" )?;
#
- e ? $str_param_VisitIp_LogFilePath )
my ? $str_VisitIp_CopyToPath ? & fun_CopyFileToCurrentAppPath ( $str_param_VisitIp_LogFilePath )?;
if ?( $str_VisitIp_CopyToPath ? eq ? #
#
my ?@ array_VisitIp_Load ?;
my ? $int_VisitIp_FileLineNums ?;
- e ? $str_VisitIp_CopyToPath )
tie ?@ array_VisitIp_Load ???? ???? ???????? 'Tie::File' ???? ???? ???????? $str_VisitIp_CopyToPath ???? ???? ???????? mode ? => ? O_RDONLY ?;
???? ???? ????????
#?
???? ???? ???? $int_VisitIp_FileLineNums ? = ?@ array_VisitIp_Load ? - ? #
my ? $str_Temp_Line ?????????? my ? $str_Temp_VisitIp ??????? my ? $str_Temp_VisitIp_Count ? 0 ??;
% hash_Temp_VisitIp ?????? = ?()?;
while ?( $int_VisitIp_FileLineNums ? >= ? ???? ???? ???? ???? $str_Temp_Line ? = ? $array_VisitIp_Load [ $int_VisitIp_FileLineNums ]?;
if ?( $str_Temp_Line ? /$time_param_Get/i )
???? ???? ???? ???? ???? /$g_str_grep_Bot/i )
???? ???? ???? ????????{
???? ???? ???? ????????????
???? ???? ???? ????????}
???? ???? ???? ???????? ???? ???? ???? ???????????? #ip
???? ???? ???? ???????????? $str_Temp_VisitIp ? & fun_GetIpByStr ( $str_Temp_Line )?;
???? ???? ???? ???????????? if ?( $str_Temp_VisitIp ? eq ? ???? ???? ???? ????????????{
???? ???? ???? ????????????}
???? ???? ???? ???????????????? #ip访()
???? ???? ???? ???????????????? if ?( exists ? $hash_Temp_VisitIp { $str_Temp_VisitIp })
???? ???? ???? ????????????????{
???? ???? ???? ???????????????????? $hash_Temp_VisitIp { $str_Temp_VisitIp }? += ? ???? ???? ???? ????????????????????
???? ???? ???? ???????????????????? #,
???? ???? ???? ???????????????????? if ?( $hash_Temp_VisitIp { $str_Temp_VisitIp }? >= ? $int_param_VisitIp_Count )
???? ???? ???? ????????????????????{
???? ???? ???? ????????????????????????
???? ???? ???? ???????????????????????? exists ? $$hash_param_VisitIp { $str_Temp_VisitIp })
???? ???? ???? ???? ???? ????????????????{
???? ???? ???? ???? ???? ???????????????????? $$hash_param_VisitIp { $str_Temp_VisitIp }? 1 ?;??
???? ???? ???? ???? ???? ????????????????}
???? ???? ???? ???? ???? ???????????????? = ? $int_param_VisitIp_Count ?;????????????????????
???? ???? ???? ????????????????????}
???? ???? ???? ????????????????}
???? ???? ???? ????????????????
last ?;
???? ???? ???? ????
???? ???? ???? ???? $int_VisitIp_FileLineNums ? -= ? 1 ?;?
#
untie ?@ array_VisitIp_Load ?;?
1 ?;???
???? ???? "fun_GetVisitIp()?$str_param_VisitIp_LogFilePath?no?found." )?;
#?End?fun_GetVisitIp()
#
#param_1?:?
#param_2?:?
sub ? fun_VisitIpSaveToCurrentPathFile ()
2 )
my ? $str_param_SaveToFileName my ?? $hash_param_Conent )? #
if ?(( length ( $str_param_SaveToFileName )? == ? 0 )? or
???????(( keys ? % $hash_param_Conent )? 0 ))
#
my ? $str_SaveToFilePath ? ????????????????????????????? $str_param_SaveToFileName )?;
- e ? $str_SaveToFilePath )
if ?( $str_SaveToFilePath ? ???? ???? ???? "fun_VisitIpSaveToCurrentPathFile()?deleteing?$str_SaveToFilePath..." )?;
unlink ( $str_SaveToFilePath );
"fun_VisitIpSaveToCurrentPathFile()?deleted?$str_SaveToFilePath." )?;
open ( HANDLE_FILE ">$str_SaveToFilePath" ))
my ? $hash_Keys ?;
my ? $hash_Values ?;
foreach ? $hash_Keys ?( sort ? % $hash_param_Conent )
???? ???? ???? $hash_Values ? = ? $$hash_param_Conent { $hash_Keys }?;
print ? HANDLE_FILE ? "$hash_Keys?=>?$hash_Values?n" ?;
close ( HANDLE_FILE )?;
#?End?fun_VisitIpSaveToCurrentPathFile()
#(.txt)
sub ? fun_IsNeedUpdate ()
my ? $str_param_FileName length ( $str_param_FileName )? ???????( % $hash_param_Conent ? my ? $bool_IsNeedUpdate ? 0 ?;? #
my ? $str_Ip ? % hash_Load_Ip ?;
my ? $str_OpenFilePath ? ???????????????????????????????????????????? $str_param_FileName )?;
#ip
- e ? $str_OpenFilePath )
open ?( HANDLE_OPEN ? $str_OpenFilePath ))
while ( my ? $str_Temp_Line ? < HANDLE_OPEN > )
???? ???? ???? ???? $str_Ip ? if ?( $str_Ip ? eq ? ???? ????????????{
???? ????????????}
???? ???????????? ???? ???????????????? #?ip
???? ???????????????? $hash_Load_Ip { $str_Ip }? 1 ?;????
close ( HANDLE_OPEN );
#
my ? $str_Temp_Value ?;
while (( $str_Ip ? $str_Temp_Value )? each ? #ip,?,?1
???????????????? exists ? $hash_Load_Ip { $str_Ip })
????????????????{
????????????????????
????????????????}
???????????????????? $bool_IsNeedUpdate ? ???????????????????? ????????????????}???
???? ????}???
#
???? ???? $bool_IsNeedUpdate ? return ? $bool_IsNeedUpdate ?;
#?End?fun_IsNeedUpdate()
#email
#param_1?:?
#param_2?:?
sub ? fun_AutoSendEmailToAlarm ()
my ? $str_param_Subject my ? $str_param_Data )? length ( $str_param_Subject )? ????????( length ( $str_param_Data )? == ?? 0 ))?
?????{
???????? 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?
????? #?End?fun_AutoSendEmailToAlarm()
#email
#param_1?:??()
sub ? fun_GetSendEmailToData ()
my ? $str_param_FileName )? #
my ? $str_Content ? ???? ???? ???? ???? chomp ( $str_Temp_Line )?;
???? ???? ???? ???? $str_Content ? .= ? $str_Temp_Line ? .= ? "n" ??;
length ( $str_Content )? return ? $str_Content ?;???
#?End?fun_GetSendEmailToData()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读