Perl简单用法:命令行参数、连接数据库mysql、DNS查询、发送邮件

时间:2023-01-01 19:47:32

一、命令行参数

1.1 代码

#!/usr/bin/env perl

use Getopt::Long;
use Data::Dumper;
use Pod::Usage;

my $Debug = 1;

sub dprint (@)
{
return unless $Debug;
chomp(my @m = @_);
print STDERR 'DEBUG: ', join(" ", @m),"\n";
}

MAIN:
{
my $db_user;
my $db_pass;

GetOptions(
\my %opt,
'debug|d+' => \$Debug,
'help|h|?' => sub{ pod2usage(-verbose=>1) },
'db_user|u=s' => \$db_user,
'db_pass|x=s' => \$db_pass,
) or pod2usage();
pod2usage(-message=>"must requires db_user, db_pass!")
unless $db_user && $db_pass;

dprint "db_user : $db_user";
dprint "db_pass : $db_pass";
}

1.2 使用

      命令:“--” 或 “-”

      ./test.pl --db_user testuser --db_pass testpass
      ./test.pl --u testuser --x testpass

Perl简单用法:命令行参数、连接数据库mysql、DNS查询、发送邮件


二、连接数据库mysql

2.1 代码

#!/usr/bin/env perl

use DBI;

MAIN:
{
my $dbh = DBI->connect("DBI:mysql:clouxns:202.85.222.121:3306", "root", "zxc1758!&%*");

$query_sql = "SELECT zone_id, zone_name FROM xns_zone";
$query_sth = $dbh->prepare($query_sql);

$query_sth->execute() or die "can't run query sql:$dbh->errstr";
while(my @data = $query_sth->fetchrow_array())
{
my $zone_id = $data[0];
my $zone_name = $data[1];
print "$zone_id\t$zone_name\n";
}

$dbh->disconnect();
}


三、DNS查询

      dns查询A记录、NS、SOA。

#!/usr/bin/env perl

use Net::DNS::Resolver;
use Net::DNS::Packet;

MAIN:
{
my $resolver = Net::DNS::Resolver->new;

#Find a host's address
my $query = $resolver->search("www.guowenyan.cn");
if($query)
{
foreach my $rr ( grep { $_->type eq "A" } $query->answer )
{
print $rr->address,"\n";
}
}
else
{
warn "query failed:",$resolver->errorstring,"\n";
}

#Find the nameservers for a domain.
my $query = $resolver->search("baidu.com", "NS");
if($query)
{
foreach my $rr ( grep { $_->type eq "NS" } $query->answer )
{
print $rr->nsdname,"\n";
}
}
else
{
warn "query failed:",$resolver->errorstring,"\n";
}

#Find a domain's SOA record in zone file format.
my $query = $resolver->search("baidu.com", "SOA");
if($query)
{
($query->answer)[0]->print;
}
else
{
warn "query failed:",$resolver->errorstring,"\n";
}
}

四、发送邮件

4.1 代码

#!/usr/bin/env perl

use Net::SMTP;

sub send_mail()
{
my @mail_to = ('480160531@qq.com', '2135361204@qq.com');

my $mail_from = 'kan_haha001@163.com';
my $mail_pass = "123zxc";
my $mail_message = "aanbb";

my $smtp = Net::SMTP->new("smtp.163.com");

$smtp->auth($mail_from, $mail_pass) || die "Auth Error! $!";
foreach my $mail_to (@mail_to)
{
$smtp->mail($mail_from);
$smtp->to($mail_to);

#Start the mail
$smtp->data();

#Send the header
$smtp->datasend("From: $mail_from\n");
$smtp->datasend("To: $mail_to\n");
$smtp->datasend("Subject: the mail is sended by perl.\n");
$smtp->datasend("\n");

#send the message
$smtp->datasend("$mail_message\n");

#send the termination string
$smtp->dataend();
}

$smtp->quit();
}
4.2 注意事项

      1. 需要安装Authen::SASL:perl -MCPAN -e shell       cpan>install Authen::SASL。

       2. 发件人、收件人必须是单引号('kan_haha001.163.com')。

      3. 邮件头中的发件人、收件人、主题、正文的前后都必须有\n。(否则会系统退信)


参考资料:

      perl发邮件:http://blog.sina.com.cn/s/blog_53eb83640100n1mo.html

     perl发送邮件:http://www.cnblogs.com/aLittleBitCool/archive/2012/08/01/2619018.html