xiaochuan 发表于 2018-8-31 09:46:38

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/
  192.168.1.2
  192.168.2.3
  192.168.5.3
  /;
  foreach my $re (@list){
  my $p = `ping $re -c 3`; icmp请求三次
  if($p=~/100% packet loss/){
  my $email = Email::Simple->create(
  header => [
  From    => 'monitor@hongshu.com',
  To      => 'monitor@hs.com',
  Subject => "$re 100% packet loss",
  ],
  body => "$re the server is down!\n",
  );
  my $sender = Email::Send->new(
  { mailer => 'Gmail',
  mailer_args => [
  username => 'monitor@hongshu.com',
  password => 'xxx',
  ]
  }
  );
  eval { $sender->send($email) };
  die "Error sending email: $@" if $@;
  }
  }

页: [1]
查看完整版本: perl编写ping脚本