热门关键字:  ubuntu  分区  函数  linux系统进程  Fedora

当前位置 :| 主页>Linux教程>编程开发>Perl>

curl+perl的自动分析程序

来源: 作者: 时间:2007-11-05 Tag: 点击:

最近单位要求写一个关于CURL用法的简单的SHELL.要求可以定制打印需要的输出项目,并且可

以对输出的内容进行自动的MAIL.乍一看,第一个定制打印还是很容易的.无非就是对HEADER里

面的信息进行GREP就OK了.不过,因为需要定制,所以打印时候难免就需要参数的组合打印.这

就需要getopts的结合使用了.赶紧翻翻书,看看用法.基本就搞定了第一个问题.

而自动MAIL的时候就出现了一个没有意想的问题.由于LINUX本机的命令mail程序需要MTA来

支持,所以简单的配置了下SENDMAIL,看看PORT 25也正常.发个信试下,发现机器内部可以发

送,但是无非向对外的MTA发送MAIL(原因为-FQDN不全,而被远端的MTA拒绝.)这下可难办

了.赶紧寻求高人指点,拔山涉水的,最后找到了宇航.给了我一段perl的代码.还真是不

赖.mail发送问题就此搞定,还不需要本地的MTA.真是爽~赶紧把写完的代码进行下总结

以同大家分享.

程序

mail.sh########curl分析脚本(主程序)

mail.pl########perl mail发送脚本 

1 mail.sh
#!/bin/bash
#file mail.sh
#version v1.1
#by IORI
#last modify 2007-11-5 14:40
#relation program mail.pl


#cat header|sed 's/ *//g' > header.ok
#rm -f header header.ok

##################HELP INFO#########################################
help()
{
 echo "usage $0  -<[D|L|T|M|C|X|A] -E<mail>> <http://TEST_URL>"
 echo "-E Receive Mail"
 echo "-D print Date"
 echo "-L print Content-Length"
 echo "-T print Content-Type"
 echo "-M print Last-Modified"
 echo "-C print Content-Encoding|vary"
 echo "-X print X-Cache|Powered-by-chinacache"
 echo "-A print ALL"
}
####################################################################

##############TEST PARAMETER######################################
if [ $# == 0 ];then
   help
   exit 1
fi
###################################################################

####################################################################
tmp=$@
URL1=`echo  $tmp  | awk '{print $NF}'`

if [ ! `echo $URL1|grep -i -E "^http"` ];then
     URL=`echo "http://$URL1"`
 else
    URL=$URL1
fi
####################################################################

####################MAIN PROGRAM#######################################

echo -n "are you sure compressed?(y/n)"
read OPT
case $OPT in
   Y|y)  /usr/bin/curl -D header -s -m 10 -compressed -o /dev/null $URL
     ;;
    N|n) /usr/bin/curl -D header -s -m 10 -o /dev/null $URL
     ;;
    \?) echo "The option is exist"
        exit 1
     ;;
esac

if [ -f header ];then
     cp -f header header.ok
 else
    echo "header isn't exist plese you check"
    exit 1
fi


HTTP=`awk '/HTTP|http/ {print $2}' header.ok`

if [ $HTTP == "200" ]||[ $HTTP == "302" ];
 then
   rm -f content
   send=0
   use=0
   #echo "==============================================================="
   # while getopts :LTMSE: OPTION
       while getopts LDTMCXAE: OPTION
       do
        case $OPTION in
         E) MAIL=$OPTARG
            send=1
             ;;
         L) cat header.ok|grep -i 'Content-Length' >> ./content
            use=1
            ;;
         T) cat header.ok|grep -i 'Content-Type' >> ./content
            use=1
            ;;
         M) cat header.ok|grep -i 'Last-Modified' >> ./content
            use=1
            ;; 
         D) cat header.ok|grep -i 'Date' >> ./content
            use=1
            ;;
         C) cat header.ok|grep -i -E "^(Vary|Content-Encoding|Accept-Encoding).*" >> ./content
            use=1
            ;;
         X) cat header.ok|grep -i -E "^X-Cache|Powered" >> ./content  ###The same as ^(|).*
            use=1
            ;;
         A) cat header.ok > ./content
            use=1
            ;;
        \?) help
            send=0
            ;;
          esac
       done
           if [ $send == 1 ]&&[ $use == 1 ] ; then
             /root/mail.pl "$MAIL" "${URL1}_test" "content"
           fi
else
   #echo "HTTP-respond code is $HTTP"
   cat header.ok >> ./content
   /root/mail.pl "iori.yang@mail.com" "HTTP_CODE_ERR" "content"
   exit 1
fi

2. mail.pl

 
#!/usr/bin/perl
#file main.pl
#version v1.0
#by IORI
#last modify 2007-11-2 15:00

use Net::SMTP;

#my $mailhost = "smtp.163.com"; # the smtp host
my $mailhost = "mail.com"; # the smtp host
my $mailfrom = 'iori.yang@mail.com'; # your email address
#my @mailto = ('fayland@gmail.com', 'not_four@hotmail.com'); # the recipient list
my @mailto = $ARGV[0]; # the recipient list
my $subject = $ARGV[1];
my $text = $ARGV[2];

open(FILE,$text) || die "Can not open list file\n";
undef $/;
$text=<FILE>;


$smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout => 120, Debug => 1);

# anth login, type your user name and password here
$smtp->auth('user','pass');

foreach my $mailto (@mailto) {
 # Send the From and Recipient for the mail servers that require it
 $smtp->mail($mailfrom);
 $smtp->to($mailto);

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

 # Send the header
 $smtp->datasend("To: $mailto
");
 $smtp->datasend("From: $mailfrom
");
 $smtp->datasend("Subject: $subject
");
 $smtp->datasend("
");

 # Send the message
 $smtp->datasend("$text

");

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

-----------------------------全文完-----------------------------------------------
                                                      BY : IORI
                                                      2007-11-5
                                                      IN chinacache

上一篇:没有了
下一篇:没有了
最新评论共有 4 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
栏目列表