我曾经为我的朋友田逸写过一个Perl脚本,主要用于Nagios服务器故障的报警。非常简单,主要使用了LWP::Simple这个模块对短信服务Api进行请求。

#!/usr/bin/perl -wuse strict;

use LWP::Simple;
use URI::Escape;
use Digest::MD5;

my ($mobile, $content) = @ARGV; # get params from command line
my $log_control = 1; # a switch of log
my $username = 'sery'; # your sms service username
my $password = 'abcdefg'; # your sms service password
my $key = 'Ysdbyhd6T'; # your sms service key
my $souce_content = substr($mobile, 0, 8) . substr($mobile, -10, 10) . $key;

my $md5 = Digest::MD5->new;
$md5->add($souce_content);
my $keyword = uc($md5->hexdigest);

my $result = get "http://http.asp.sh.cn/MT.do?Username=$username&
Password=$password&Mobile=$mobile&Content=$content&Keyword=$keyword";

if($log_control) {
my $fh;
open($fh, '>> /var/log/sms.log') or die "can't open log: $!"; # make sure have permission
print $fh join(' ', time, $result, "/n");
close $fh;
}