perlのいくつかのモジュールが使用する.
12183 ワード
perlCPANモジュールDBI.DBD::Mysql
perl標準モジュールNet::PingとIO::Socket
perl標準モジュールにおけるFIle::Findの使用方法.
perl標準モジュールNet::SMTPと依存CPANモジュールNet::SMTP_auth認証モジュール
- 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
";
- # }
- $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
$sqlstate
";
- if ($iostate eq 'No' || $sqlstate eq 'No'){
- warn "Mysql Slave database down..
";
- }
- }
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
" : print "$host: down
";
- my $sock = IO::Socket::INET->new(
- Timeout => 4,
- PeerAddr => $host,
- Peerport => $port,
- Proto => "tcp",
- );
- $sock ? print "$port: Listening
" : print "$port: faild
";
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
";
- }
- }
- }
- 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 = '[email protected]';
- my $mail_user_to = '[email protected]';
- 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
";
- $smtp->auth("$mail_user_from","$mail_user_pass") or die "auth failed!
";
- $smtp->mail("$mail_user_from");
- $smtp->to("$mail_user_to");
- $smtp->data();
- $smtp->datasend("mail test!!
");
- $smtp->datasend("donghui
");
- $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
";
- $exp->log_file("ssh_host.log","w");
- $exp->expect(3,[qr/connecting \(yes\/no\)/i,
- sub{
- my $self = shift;
- $self->send("yes
");
- exp_continue;
- }],
- [
- qr/password:/i,
- sub{
- my $self = shift;
- $self->send("$pass
");
- exp_continue;
- }]
- );
- $exp->send("netstat -ntpl
") if ($exp->expect(undef,'#'));
- $exp->send("exit
") if($exp->expect(undef,'#'));
- $exp->log_file(undef);