perlCPAN模块DBI.DBD::Mysql

 
  
  1. my $db_host = "localhost"
  2.  my $db_port = "3306"
  3.  my $db_user = "root"
  4.  my $db_pass = "redhat"
  5.  my $db_name = "test"
  6.  my $dsn = "DBI:mysql:$db_name:$db_host:$db_port"
  7.  my $dbh = DBI->connect($dsn,$db_user,$db_pass,{RaiseError => 0,PrintError => 0}) or return ("0"); 
  8.     $dbh->do("set names gbk"); 
  9. #   my $sth = $dbh->prepare("select * from mysql.user"); 
  10. #      $sth->execute(); 
  11. #               while(my @row = $sth->fetchrow_array){ 
  12.                 #       print "@row\n"
  13.  #   } 
  14.         $sth = $dbh->prepare("SHOW SLAVE STATUS"); 
  15.         $sth->execute(); 
  16.                 while(my $hash=$sth->fetchrow_hashref){ 
  17.                         my $iostate = $hash->{'Slave_IO_Running'}; 
  18.                         my $sqlstate = $hash->{'Slave_SQL_Running'}; 
  19. #       print "$iostate\n$sqlstate\n"
  20.                                 if ($iostate eq 'No' || $sqlstate eq 'No'){ 
  21.                                         warn "Mysql Slave database down..\n"
  22.     } 
  23.   } 

 

perl标准模块Net::Ping和IO::Socket

 
  
  1. #!/usr/bin/perl 
  2. use warnings; 
  3. use strict; 
  4. use IO::Socket; 
  5. use Net::Ping; 
  6. my $host = "192.168.1.2"
  7. my $port = "80"
  8. my $p=Net::Ping->new("icmp"); 
  9. $p->ping($host,5) ? print "$host: runing\n" : print "$host: down\n"
  10. my $sock = IO::Socket::INET->new( 
  11.                         Timeout => 4, 
  12.                         PeerAddr => $host, 
  13.                         Peerport => $port, 
  14.                         Proto   => "tcp"
  15. ); 
  16. $sock ? print "$port: Listening\n" : print "$port: faild\n"

perl标准模块中FIle::Find的使用方法.

 
  
  1. #!/usr/bin/perl 
  2. use warnings; 
  3. use strict; 
  4. use File::Find; 
  5. my $path="/etc/"
  6. sub wanted{ 
  7.         my $file=$File::Find::name
  8.         if(-f $file and -s $file > 5000 and -s $file < 10000){ 
  9.         if($file =~m/\.conf$/){ 
  10.         print "$file\n"
  11.   } 
  12.  } 
  13. find(\&wanted,$path); 

perl标准模块Net::SMTP和依赖CPAN模块Net::SMTP_auth认证模块.

 
  
  1. #!/usr/bin/perl 
  2. use warnings; 
  3. #use strict; 
  4. use Net::SMTP; 
  5. use Net::SMTP_auth; 
  6. my $smtp_mail_host = 'smtp.sinanet.com'
  7. my $mail_user_from = 'donghui@leju.sina.com.cn'
  8. my $mail_user_to = 'donghui@leju.sina.com.cn'
  9. my $mail_user_pass = "P@ssW0rd"
  10. my $mail_helo = 'mail.sinanet.com'
  11. $smtp = Net::SMTP->new( 
  12.                 Host => "$smtp_mail_host"
  13.                 Hello => "$mail_helo"
  14.                 Timeout => 40, 
  15.                 Debug => 1, 
  16. or die "can not connect mail server\n"
  17. $smtp->auth("$mail_user_from","$mail_user_pass"or die "auth failed!\n"
  18. $smtp->mail("$mail_user_from"); 
  19. $smtp->to("$mail_user_to"); 
  20. $smtp->data(); 
  21. $smtp->datasend("mail test!!\n"); 
  22. $smtp->datasend("donghui\n"); 
  23. $smtp->dataend(); 
  24. $smtp->quit(); 

perl中远程执行命令CPAN模块:Expect

 
  
  1. #!/usr/bin/perl 
  2. use warnings; 
  3. use strict; 
  4. use Expect; 
  5. my $host = "192.168.1.2"
  6. my $pass = "redhat"
  7.  $ENV{'TERM'} = "xterm"
  8. my $exp = Expect->new; 
  9.    $exp->log_stdout(0); 
  10.    $exp = Expect->spawn("ssh -l root $host"or die "can't conenct $host\n"
  11.    $exp->log_file("ssh_host.log","w"); 
  12.    $exp->expect(3,[qr/connecting \(yes\/no\)/i, 
  13.                         sub{ 
  14.                                 my $self = shift; 
  15.                                 $self->send("yes\n"); 
  16.                                 exp_continue; 
  17.  }], 
  18.         qr/password:/i, 
  19.         sub{ 
  20.                 my $self = shift; 
  21.                 $self->send("$pass\n"); 
  22.                 exp_continue; 
  23.  }] 
  24. ); 
  25. $exp->send("netstat -ntpl\n") if ($exp->expect(undef,'#')); 
  26. $exp->send("exit\n") if($exp->expect(undef,'#')); 
  27. $exp->log_file(undef);