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 "$file\n";
[*]}
[*] }
[*]}
[*]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 = "P@ssW0rd";
[*]my $mail_helo = 'mail.sinanet.com';
[*]$smtp = Net::SMTP->new(
[*] Host => "$smtp_mail_host",
[*] Hello => "$mail_helo",
[*] Timeout => 40,
[*] Debug => 1,
[*]) or die "can not connect mail server\n";
[*]$smtp->auth("$mail_user_from","$mail_user_pass") or die "auth failed!\n";
[*]$smtp->mail("$mail_user_from");
[*]$smtp->to("$mail_user_to");
[*]$smtp->data();
[*]$smtp->datasend("mail test!!\n");
[*]$smtp->datasend("donghui\n");
[*]$smtp->dataend();
[*]$smtp->quit();
perl中远程执行命令CPAN模块:Expect
[*]#!/usr/bin/perl
[*]use warnings;
[*]use strict;
[*]use Expect;
[*]my $host = "192.168.1.2";
[*]my $pass = "redhat";
[*] $ENV{'TERM'} = "xterm";
[*]my $exp = Expect->new;
[*] $exp->log_stdout(0);
[*] $exp = Expect->spawn("ssh -l root $host") or die "can't conenct $host\n";
[*] $exp->log_file("ssh_host.log","w");
[*] $exp->expect(3,[qr/connecting \(yes\/no\)/i,
[*] sub{
[*] my $self = shift;
[*] $self->send("yes\n");
[*] exp_continue;
[*] }],
[*][
[*] qr/password:/i,
[*] sub{
[*] my $self = shift;
[*] $self->send("$pass\n");
[*] exp_continue;
[*] }]
[*]);
[*]$exp->send("netstat -ntpl\n") if ($exp->expect(undef,'#'));
[*]$exp->send("exit\n") if($exp->expect(undef,'#'));
[*]$exp->log_file(undef);
页:
[1]