【转】perl ping检测功能脚本代码
2024-08-22 00:45:16
我的第一个用于生产环境的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 => 'monitor@a.com',
To => 'monitor@b.com',
Subject => "$re 100% packet loss",
],
body => "$re the server is down!\n",
);
my $sender = Email::Send->new(
{ mailer => 'Gmail',
mailer_args => [
username => 'monitor@a.com',
password => 'xxx',
]
}
);
eval { $sender->send($email) };
die "Error sending email: $@" if $@;
}
}
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 => 'monitor@a.com',
To => 'monitor@b.com',
Subject => "$re 100% packet loss",
],
body => "$re the server is down!\n",
);
my $sender = Email::Send->new(
{ mailer => 'Gmail',
mailer_args => [
username => 'monitor@a.com',
password => 'xxx',
]
}
);
eval { $sender->send($email) };
die "Error sending email: $@" if $@;
}
}
原文链接:http://www.jb51.net/article/33965.htm
最新文章
- ubuntu 16.04 Dockerfile 安装mysql
- python高级之面向对象高级
- <;<;<; Tomcat 部署项目There are no resources that can be added or removed from the server
- two day python基础知识
- java 的数据类型
- linux之unlink函数解析
- 解决VS2013中“This function or variable may be unsafe”的问题
- Win7删除右键菜单中“图形属性”和“图形选项”
- 带着新人学springboot的应用05(springboot+RabbitMQ 上)
- Failed to create the XA control connection. Error: ";找不到存储过程 &#39;master..xp_sqljdbc_xa_init_ex&#39;。
- Python全栈学习_day007作业
- DFS基础题
- C++中 线程函数为静态函数 及 类成员函数作为回调函数
- django面试三
- VC 字符串转化和分割
- luasocket 接收数据
- SQLAlchemy并发写入引发的思考
- 【Docker】第四篇 Docker仓库管理
- [POI2012]STU-Well
- 牛客练习赛9 B - 珂朵莉的值域连续段
热门文章
- [置顶] 《MFC游戏开发》笔记一 系列简介
- MPMoviePlayerController过期导致视频播放时间条颠倒及AVPlayerViewController的用法
- CentOS-6.5安装配置Tomcat7
- zabbix电话告警V1
- [转]在SQL中用正则表达式替换html标签
- LeetCode 260
- [BigData]关于HDFS的伪分布式安装和虚拟机网络的配置
- MultiMap、BidiMap及LazyMap的使用
- RedirectFromLoginPage和FormsAuthenticationTicket的区别
- Android:Xml(读取与存储)