老爷子88 发表于 2018-8-31 13:13:11

perl中的几个模块使用.

perlCPAN模块DBI.DBD::Mysql
  


[*] my $db_host = "localhost";
[*] my $db_port = "3306";
[*] my $db_user = "root";
[*] my $db_pass = "redhat";
[*] my $db_name = "test";
[*] my $dsn = "DBI:mysql:$db_name:$db_host:$db_port";
[*] my $dbh = DBI->connect($dsn,$db_user,$db_pass,{RaiseError => 0,PrintError => 0}) or return ("0");
[*]    $dbh->do("set names gbk");
[*]#   my $sth = $dbh->prepare("select * from mysql.user");
[*]#      $sth->execute();
[*]#               while(my @row = $sth->fetchrow_array){
[*]                #       print "@row\n";
[*] #   }
[*]      $sth = $dbh->prepare("SHOW SLAVE STATUS");
[*]      $sth->execute();
[*]                while(my $hash=$sth->fetchrow_hashref){
[*]                        my $iostate = $hash->{'Slave_IO_Running'};
[*]                        my $sqlstate = $hash->{'Slave_SQL_Running'};
[*]#       print "$iostate\n$sqlstate\n";
[*]                              if ($iostate eq 'No' || $sqlstate eq 'No'){
[*]                                        warn "Mysql Slave database down..\n";
[*]    }
[*]}
  

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


[*]#!/usr/bin/perl
[*]use warnings;
[*]use strict;
[*]use IO::Socket;
[*]use Net::Ping;
[*]my $host = "192.168.1.2";
[*]my $port = "80";
[*]my $p=Net::Ping->new("icmp");
[*]$p->ping($host,5) ? print "$host: runing\n" : print "$host: down\n";
[*]my $sock = IO::Socket::INET->new(
[*]                        Timeout => 4,
[*]                        PeerAddr => $host,
[*]                        Peerport => $port,
[*]                        Proto   => "tcp",
[*]);
[*]$sock ? print "$port: Listening\n" : print "$port: faild\n";
  

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


[*]#!/usr/bin/perl
[*]use warnings;
[*]use strict;
[*]use File::Find;
[*]my $path="/etc/";
[*]sub wanted{
[*]      my $file=$File::Find::name;
[*]      if(-f $file and -s $file > 5000 and -s $file < 10000){
[*]      if($file =~m/\.conf$/){
[*]      print &quot;$file\n&quot;;
[*]}
[*] }
[*]}
[*]find(\&wanted,$path);
  

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


[*]#!/usr/bin/perl
[*]use warnings;
[*]#use strict;
[*]use Net::SMTP;
[*]use Net::SMTP_auth;
[*]my $smtp_mail_host = 'smtp.sinanet.com';
[*]my $mail_user_from = 'donghui@leju.sina.com.cn';
[*]my $mail_user_to = 'donghui@leju.sina.com.cn';
[*]my $mail_user_pass = &quot;P@ssW0rd&quot;;
[*]my $mail_helo = 'mail.sinanet.com';
[*]$smtp = Net::SMTP->new(
[*]                Host => &quot;$smtp_mail_host&quot;,
[*]                Hello => &quot;$mail_helo&quot;,
[*]                Timeout => 40,
[*]                Debug => 1,
[*]) or die &quot;can not connect mail server\n&quot;;
[*]$smtp->auth(&quot;$mail_user_from&quot;,&quot;$mail_user_pass&quot;) or die &quot;auth failed!\n&quot;;
[*]$smtp->mail(&quot;$mail_user_from&quot;);
[*]$smtp->to(&quot;$mail_user_to&quot;);
[*]$smtp->data();
[*]$smtp->datasend(&quot;mail test!!\n&quot;);
[*]$smtp->datasend(&quot;donghui\n&quot;);
[*]$smtp->dataend();
[*]$smtp->quit();

  

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


  


[*]#!/usr/bin/perl
[*]use warnings;
[*]use strict;
[*]use Expect;
[*]my $host = &quot;192.168.1.2&quot;;
[*]my $pass = &quot;redhat&quot;;
[*] $ENV{'TERM'} = &quot;xterm&quot;;
[*]my $exp = Expect->new;
[*]   $exp->log_stdout(0);
[*]   $exp = Expect->spawn(&quot;ssh -l root $host&quot;) or die &quot;can't conenct $host\n&quot;;
[*]   $exp->log_file(&quot;ssh_host.log&quot;,&quot;w&quot;);
[*]   $exp->expect(3,[qr/connecting \(yes\/no\)/i,
[*]                        sub{
[*]                              my $self = shift;
[*]                              $self->send(&quot;yes\n&quot;);
[*]                              exp_continue;
[*] }],
[*][
[*]      qr/password:/i,
[*]      sub{
[*]                my $self = shift;
[*]                $self->send(&quot;$pass\n&quot;);
[*]                exp_continue;
[*] }]
[*]);
[*]$exp->send(&quot;netstat -ntpl\n&quot;) if ($exp->expect(undef,'#'));
[*]$exp->send(&quot;exit\n&quot;) if($exp->expect(undef,'#'));
[*]$exp->log_file(undef);


页: [1]
查看完整版本: perl中的几个模块使用.