Chinaunix首页 | 论坛 | 博客
  • 博客访问: 314869
  • 博文数量: 75
  • 博客积分: 1956
  • 博客等级: 上尉
  • 技术积分: 862
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-21 15:36
文章分类

全部博文(75)

文章存档

2018年(1)

2012年(3)

2011年(19)

2010年(1)

2009年(1)

2008年(50)

分类: LINUX

2011-12-01 15:31:33

#!/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();

阅读(2231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~