#!/usr/bin/perl
use Device::Modem;
use Device::Gsm;
use DBI;
use encoding "utf-8";
use Unicode::String qw(utf8);
use strict;
########时间###########
my @time=localtime();
my $year=@time[5]+1900;
my $mon=@time[4]+1;
my $day=@time[3];
my $date=$year.'-'.$mon.'-'.$day;
my $time=@time[2].':'.@time[1].':'.@time[0];
##########连接数据库#################
my $database = 'sms';
my $hostname = 'localhost';
my $user = 'nagios';
my $password = 'P@ssw0rd';
my $dsn = "DBI:mysql:database=$database;host=$hostname";
my $dbh = DBI->connect($dsn, $user, $password);
$dbh->{'mysql_enable_utf8'}=1;
my $drh = DBI->install_driver("mysql");
########连接modem##########
my $sms = new Device::Modem(port=>'/dev/ttyS0'
);
if($sms->connect( baudrate=>115200 )){
# print "Modem Connect OK \n";
}else{
print "Modem Connect ERROR \n";
}
############数据库查询#######################
my $sth = $dbh->prepare("SELECT * FROM sms_send WHERE state='0' AND date=\'$date\'");
$sth->execute;
my $numRows = $sth->rows;
if($numRows != 0){
while ( my $ref = $sth->fetchrow_hashref() )
{
print "servie id=".$ref->{'id'}.'-'. $ref->{'num'} .'-'. $ref->{'context'} .'-'.$ref->{'state'}.' - '.$ref->{'date'}.' - '.$ref->{'time'}."\n";
my $sms_cont = $ref->{'context'};
my $num = $ref->{'num'};
my $res = 'AT+CMGS='.'"'.$num.'"';
#######UTF8编码#########
my $u = utf8($sms_cont);
my @u1 = split(/ /,$u->hex);
my $i;
foreach $i (@u1){
$i=substr($i,2,5);
}
my $u2 = join('',@u1);
##########发送短信################
my $u_ll = length($u2);
my $feet = 280;
for ($i=0;$i<$u_ll;$i=$i+$feet){
my $text = substr($u2,$i,$feet);
#my $text=$u2;
$sms->atsend( $res . Device::Modem::CR );
my $line = $sms->answer('>',1000) ."\n";
$sms->atsend( $text . Device::Modem::CTRL_Z);
$line = $sms->answer('\+CMGS',6000) ."\n";
}
sleep (5);
$dbh->do("UPDATE sms_send SET state='1' WHERE id=".$ref->{id});
}
print "All Message Sent. \n";
}else
{
#print "No Message To Send. \n";
}
$sth->finish;
########断开数据库和Modem################
$dbh->disconnect();
$sms->disconnect();
阅读(2221) | 评论(0) | 转发(0) |