perl ping检测功能脚本代码

我的第一个用于生产环境的perl脚本,虽然不是很优秀,但也迈出了扎实的一步 :)
领导有任务,给一批IP列表,ping每一台机器,如果没有响应就发邮件通知,通知的邮件需要分开,不能通知一个列表,得一封一封的通知.
用到email::send模块,因为需要用到Gmail

代码如下:

#!/usr/bin/perl 
use warnings; 
use strict; 
use Email::Send; 
use Email::Send::Gmail; 
use Email::Simple::Creator; 
my @list = qw/ 
1.1.1.1 
2.2.2.2 
3.3.3.3 
/; 
foreach my $re (@list){ 
        my $p = `ping $re -c 3`; 
        if($p=~/100% packet loss/){ 
                my $email = Email::Simple->create( 
                header => [ 
                From    => '[email protected]', 
                To      => '[email protected]', 
                Subject => "$re 100% packet loss", 
], 
body => "$re the server is down!\n", 
 ); 
                my $sender = Email::Send->new( 
                { mailer => 'Gmail', 
                mailer_args => [ 
                username => '[email protected]', 
                password => 'xxx', 
                ] 
                } 
); 
        eval { $sender->send($email) }; 
        die "Error sending email: $@" if $@; 
        } 
}

相关推荐