perl通过网关发短信程序

sub send_phone_msg { my $NOWTIME = `/bin/date +'%d %k:%M:%S'`; chomp($NOWTIME); my $A='$1'; my $B='$2'; my $IP=`/sbin/ifconfig |grep "inet addr" |grep -v 127.0.0 |awk -F ":" '{print $B}' |awk '{print $A}'|head -1`; chomp($IP); my $remote_host="*.*.*.*"; my $remote_host_back="*.*.*.*"; my $remote_port=*; my $timeout=3; my $ALARMID=shift; #手机号 my $ALARMMSG=shift; #信息 my $len=length($ALARMMSG); #处理中文 my $i; my @tmp; for($i=0;$i<$len;$i++) { $tmp[$i]=substr($ALARMMSG,$i,1); } my $message=""; for($i=0;$i<$len;$i++) { if( $tmp[$i] eq "&") { my $t=sprintf("%X",ord($tmp[$i])); $message.="%%"; } else { $message.=$tmp[$i]; } } #处理中文结束 my $socket; $socket=IO::Socket::INET->new(PeerAddr => $remote_host,PeerPort => $remote_port,Proto => "tcp",Type => SOCK_STREAM,Timeout=> $timeout) or $socket=IO::Socket::INET->new(PeerAddr => $remote_host_back,PeerPort => $remote_port,Proto => "tcp",Type => SOCK _STREAM,Timeout=> $timeout) or die "connect $remote_host $remote_port failed $!"; my $str="${NOWTIME}&${ALARMID}&${IP}&${message}/r/n"; my $ret=$socket->send($str); my $recData; $socket->recv($recData,100,0); my @rec=split("&",$recData,3); my $rec1=$rec[0]; my $rec2=$rec[2]; my $msglen=$rec[1]; my $recmsg=substr($rec2,0,$msglen); if($rec1==0){print "send success $recmsg/n";} else {print "send failed ret=$rec1 msg=$recmsg/n";} $socket->shutdown(2); }  

 

你可能感兴趣的:(IO,tcp,Stream,socket,perl,手机)