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 $@; } }
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06