89ou 发表于 2018-8-31 11:07:58

Perl 模块(2)

  1.Net::telnet 模块
  use Net::telnet;
  $telnet=Net::telnet->new(192.168.1.1);
  $telnet->login(user,pass);
  @lines=$telnet->cmd('ps -ef');
  print @lines;
  ==================================
  2. Net::SMTP_auth 模块
  use Net::SMTP_auth;
  open(AI,'d:\test.txt');
  $smtp = Net::SMTP_auth->new('smtp.163.com',
  Hello => '163.com',
  Debug => 1);
  $smtp->auth('LOGIN', 'a@163.com', 'xxxxx');
  $smtp->mail('a@163.com');
  $smtp->to('a@163.com');
  $smtp->data();
  $smtp->datasend("From:a\@163.com \n");
  $smtp->datasend("To: a\@163.com \n");
  $smtp->datasend("Subject: Test Mail \n");
  $smtp->datasend("\n");
  while () {
  $smtp->datasend("$_");
  }
  $smtp->dataend();
  $smtp->quit;
  ================================================
  3.Net::SMTP   模块
  use Net::SMTP;
  $mail_server = 'smtp.163.com';
  $mail_from = 'a@163.com';
  $mail_to = 'b@163.com';
  $uname='a@163.com';
  $passwd='xxxxx';
  #开启Debug模式
  $smtp = Net::SMTP->new("$mail_server" , Debug => 1);
  #普通发送模式
  #$smtp = Net::SMTP->new("$mail_server" );
  $smtp->auth("$uname", "$passwd");
  $smtp->mail("$mail_from");
  $smtp->to("$mail_to");
  $smtp->data();
  $smtp->datasend("To: $mail_from\n");
  $smtp->datasend("Cc: 'me@163.com', 'me2@gmail.com'\n"); # 可抄送给多个人
  $smtp->datasend("From: $mail_from\n");
  $smtp->datasend("Reply-To: $mail_from\n");
  $smtp->datasend("Return-Path: $mail_from\n");
  $smtp->datasend("Subject: 1 Subject\n");
  #$smtp->datasend("Content-Type: text/plain; ChartSet=gb2312");
  $smtp->datasend("\n"); # 解决了正文没有显示的问题
  # Send your email content
  $smtp->datasend("I am a cat!\n");
  $smtp->datasend("\n");
  $smtp->dataend();
  $smtp->quit;
  实例:
  use strict;
  use warnings;
  use Win32::OLE;
  open(DF,'//192.168.1.5/test/df.txt') or die "Cannot open: df.txt\n";
  my @vol=;
  my $app_xls=Win32::OLE->new('Excel.Application') or die "Cannot install excel\n";
  #$app_xls->{DisplayAlerts}='False';
  my $src_book=$app_xls->WorkBooks->Open('C:\scripts\120713-From-空間-備份-CGXM.xlsx') or die "Can not open file\n";
  my $src_sheet=$src_book->Worksheets(1) or die "Can not open sheet\n";
  $src_sheet->Cells(57,'E')->{'Value'}=int($vol/1024/1024);      # Mirror2 OS
  $src_sheet->Cells(58,'E')->{'Value'}=int($vol/1024/1024);   # Mirror2 bkup3
  $src_sheet->Cells(54,'E')->{'Value'}=int($vol/1024/1024);      # Mirror OS
  $src_sheet->Cells(55,'E')->{'Value'}=int($vol/1024/1024);   # Mirror bkup
  $src_sheet->Cells(56,'E')->{'Value'}=int($vol/1024/1024);   # Mirror bkup2
  $src_sheet->Cells(59,'E')->{'Value'}=int($vol/1024/1024);      # Backup OS
  $src_sheet->Cells(8,'E')->{'Value'}=int($vol/1024/1024);       # Data1 OS
  $src_sheet->Cells(9,'E')->{'Value'}=int($vol/1024/1024);    # Data1_db1A
  $src_sheet->Cells(15,'E')->{'Value'}=int($vol/1024/1024);      # Data2_OS
  $src_sheet->Cells(16,'E')->{'Value'}=int($vol/1024/1024);# Data2_db2A
  $src_sheet->Cells(21,'E')->{'Value'}=int($vol/1024/1024);# Data2_db2B
  $src_sheet->Cells(23,'E')->{'Value'}=int($vol/1024/1024);   # Data3_OS
  $src_sheet->Cells(24,'E')->{'Value'}=int($vol/1024/1024);# Data3_db3A
  $src_sheet->Cells(26,'E')->{'Value'}=int($vol/1024/1024);# Data3_db3B
  $src_sheet->Cells(27,'E')->{'Value'}=int($vol/1024/1024);   # Data3_db3C
  $src_sheet->Cells(29,'E')->{'Value'}=int($vol/1024/1024);# Data3_db3D
  $src_sheet->Cells(31,'E')->{'Value'}=int($vol/1024/1024);   # Data4_OS
  $src_sheet->Cells(32,'E')->{'Value'}=int($vol/1024/1024);# Data4_db4A
  $src_sheet->Cells(33,'E')->{'Value'}=int($vol/1024/1024);# Data4_db4B
  $src_sheet->Cells(34,'E')->{'Value'}=int($vol/1024/1024);# Data4_db4C
  $src_sheet->Cells(35,'E')->{'Value'}=int($vol/1024/1024);# Data4_db4D
  $src_sheet->Cells(39,'E')->{'Value'}=int($vol/1024/1024);   # Frames1_OS
  $src_sheet->Cells(40,'E')->{'Value'}=int($vol/1024/1024);# Frames1_f1A
  $src_sheet->Cells(42,'E')->{'Value'}=int($vol/1024/1024);   # Frames1_f1B
  $src_sheet->Cells(44,'E')->{'Value'}=int($vol/1024/1024);   # Frames1_f1C
  $src_sheet->Cells(45,'E')->{'Value'}=int($vol/1024/1024);# Frames1_f1D
  $src_sheet->Cells(46,'E')->{'Value'}=int($vol/1024/1024);   # Frames2_OS
  $src_sheet->Cells(47,'E')->{'Value'}=int($vol/1024/1024);# Frames2_f2A
  $src_sheet->Cells(50,'E')->{'Value'}=int($vol/1024/1024);# Frames2_f2B
  $src_book->Save;
  #$app_xls->{DisplayAlerts}='True';
  #$src_book->Close;
  undef $app_xls;
  undef $src_book;
  my $KillExcel = `taskkill /F /IM EXCEL.EXE 2>> nul`;
  chomp $KillExcel;
  print "结束Excel程序:$KillExcel\n" if ($KillExcel);
  sleep 2;
  ============================================
  4. Email::Valid模块          (验证邮件的有效性)
  use strict;
  use warnings;
  use Email::Valid;
  my $email_address='zhaod06211@example.';
  unless (Email::Valid->address($email_address)) {
  print "Sorry,that mail address is not valid\n";
  }
  =================================================
  5.Spreadsheet::WriteExcel模块
  use Spreadsheet::WriteExcel;
  use strict;
  my $filename='D:\a.txt';
  open(DF,$filename) || die "Cannot open file: $!";
  my @a=;
  my $use=$a;
  my $add=$a;
  my $res=$a;
  my $xls=Spreadsheet::WriteExcel->new('perl.xls');
  my $xlsContent=$xls->add_worksheet('report');
  my $format = $xls->add_format();
  $format->set_size(11);
  $format->set_bold();
  $format->set_color('red');
  $format->set_align('center');
  $xlsContent->write("A1",$use,$format);
  $xlsContent->write("B1",$res,$format);
  $xlsContent->write("C1",$add,$format);
  $xls->close();
  ================================================
  Win32::OLE模块
  use strict;
  use warnings;
  use Win32::OLE;   #导入win32 OLE包
my $dir = 'D://myperl//win32ole//';#在此目录下新建两个EXCEL  my $src_name = $dir."test1"."/.xls";
  my $dst_name = $dir."test2"."/.xls";
my $nowstr;  #新建一个EXCEL应用对象,然后我们就可以对excel进行操作。
  my $app_xls = Win32::OLE->new('Excel.Application', sub{$_->Quit})
  or die"Can't install Excel01!";
#打开一个EXCEL文件,'True' 表示是只读my $src_book = $app_xls->WorkBooks->Open($src_name, 0, 'True');  my $src_sheet = $src_book->Worksheets(1); #选中一工作表
  $nowstr = $src_sheet->Cells(1,'A')->{Value};#取得一单元格中数据
  print($nowstr);
  $src_sheet->Cells(1,'A')->{Value}="change";#修改一单元格中数据
  my $dst_book = $app_xls->WorkBooks->Open($dst_name);
  my $dst_sheet = $dst_book->Worksheets(1);
  $app_xls->{DisplayAlerts} = 'False';    #关掉excel的提示,比如是否保存修改之类的
  #$dst_book->Worksheets(1)->Delete; #删除一工作表
  #从一个excel里复制一工作表到另一excel中
  $src_book->Worksheets(1)->Copy($dst_book->Worksheets('sheet3'));
  $dst_book->Save;#保存所做修改
  $app_xls->{DisplayAlerts} = 'True'; #恢复提示
undef $src_book;  undef $dst_book;
undef $app_xls;#关掉所打开的excel应用  ===============================================================
  Net::SNMP 模块
  use strict;
  use Net::SNMP;
  my $OID_Mail_C='1.3.6.1.2.1.25.2.3.1.6.1';# 查询windows磁盘容量的使用情况 OID
  my $OID_Mail_D='1.3.6.1.2.1.25.2.3.1.6.2';
  my $OID_Mail_E='1.3.6.1.2.1.25.2.3.1.6.3';
  my $OID_Mail_F='1.3.6.1.2.1.25.2.3.1.6.4';
  my ($session,$error) = Net::SNMP->session(
  -hostname => '192.168.1.3',
  -community => 'public',
  -version => 'snmpv2c',
  );
  if (! defined $session) {
  printf "ERROR: %s.\n", $error;
  exit 1;
  }
  my $result = $session->get_request(-varbindlist => [$OID_Mail_C,$OID_Mail_D,$OID_Mail_E,$OID_Mail_F]);
  if (!defined $result) {
  printf "ERROR: %s.\n", $session->error();
  $session->close();
  exit 1;
  }
  my $Mail_C_use=int(($result->{$OID_Mail_C} * 4096) / 1024/1024/1024); # byte 转换为 G
  my $Mail_D_use=int(($result->{$OID_Mail_D} * 4096) / 1024/1024/1024);
  my $Mail_E_use=int(($result->{$OID_Mail_E} * 4096) / 1024/1024/1024);
  my $Mail_F_use=int(($result->{$OID_Mail_F} * 4096) / 1024/1024/1024);
  print $Mail_E_use;
  ===================================================
  IO::Socket模块 (监控服务是否正常并email)
  #! /usr/bin/perl
  #use strict;
  use IO::Socket;
  use Net::SMTP;
  #@time=localtime();
  #print "@time";
  # Mail
  use constant SMB_PORT => (139,445);
  use constant SMB_SERVER => ('192.168.1.224');
  ##### Check server port is alive ######
  sub check_server_port {
  my($server,$port)=@_;
  $sock=IO::Socket::INET->new(PeerAddr=>$server,PeerPort=>$port,Proto=>'tcp');
  unless ($sock) {
  return 0;
  }
  use constant SMB_PORT => (139,445);
  use constant SMB_SERVER => ('192.168.1.224');
  ##### Check server port is alive ######
  sub check_server_port {
  my($server,$port)=@_;
  $sock=IO::Socket::INET->new(PeerAddr=>$server,PeerPort=>$port,Proto=>'tcp');
  unless ($sock) {
  return 0;
  }
  $sock->close();
  return 1;
  }
  ##### Send Mail #####
  sub mail {
  $mail_server='mail.cgxm.com';
  $mail_from='zhaod@xxx.com.cn';
  $mail_to='zhaod@xxx.com.cn';
  $username='zhaod';
  $passwd='xxx.';
  ($subject,$message)=@_;
  $smtp=Net::SMTP->new("$mail_server",debug=>1);
  $smtp->auth("$username","$passwd");
  $smtp->mail("$mail_from");
  $smtp->to("$mail_to");
  $smtp->data();
  $smtp->datasend("To: $mail_to\n");
  $smtp->datasend("From: $mail_from\n");
  $smtp->datasend("Subject: $subject\n");
  $smtp->datasend("\n");
  $smtp->datasend("\n$message");
  $smtp->dataend();
  $smtp->quit();
  }
  ##### Check server is alive mail #####
  sub monitor_main {
  # check Mail
  foreach $item (SMB_SERVER) {
  foreach $port (SMB_PORT) {
  if (!check_server_port($item,$port)) {
  mail('Wraning',"$item
  samba服务停止运行,请处理.")
  } else {
  print "smb server is up\n";
  }
  }
  }
  }
  monitor_main()

页: [1]
查看完整版本: Perl 模块(2)