很简单、很初级的一个perl程序,功能为将指定文件的内容,以socket方式发送到接收端。
#!/usr/bin/perl
use IO::Socket;
use strict;
#指定日志、配置文件路径
my $log_file="/wls/monitoropr/pubweb/log/sendres.log";
my $config_file="/wls/monitoropr/pubweb/perl/conf/pubweb.conf";
my $title;
my $servername;
my $file;
#
写日志的方法
sub wrt_log
{
my ($msg,$log_file)=@_;
my $time = `date +%D-%T`;
chomp($time);
open my $wrt,">>$log_file";
print $wrt "$time >".$msg."\n";
close $wrt;
}
#socket通信的方法
sub send_socket{
my $error="";
open(my $hand,"$file");
#@array=<$hand>;
while(my $res=<$hand>){
$error=$error.$res;
}
close($hand);
if($error){
#向指定socket发送消息
eval{
my $socket = IO::Socket::INET->new(PeerAddr => "10.11.100.136:29999",
Proto => "tcp",
Type => IO::Socket::SOCK_STREAM,
Timeout => 300);
if(defined($socket))
{
my @now = localtime(time);
my $nowTime = sprintf('%04d%02d%02d%02d%02d%02d',$now[5]+1900, $now[4]+1, $now[3], $now[2], $now[1], $now[0]);
my $error_info='
'.$title.''.$nowTime.''.'server_name:'.$servername.",exception_log:\n".$error.'';
print $socket $error_info;
$socket->close();
}
};
}
}
#读配置文件,调用send_socket方法发送指定消息(读配置部分可用散列改写)
my $flag;
open(my $conf,"$config_file");
while(my $line=<$conf>){
chomp($line);
next if ($line eq "##start##" || $line eq "");
$flag = 1 if $line eq "##end##";
my @item=split(/=/,$line);
$title=$item[1] if $item[0] eq "title";
$servername=$item[1] if $item[0] eq "servername";
$file=$item[1] if $item[0] eq "file";
continue:{ #当匹配到##end##时,调用send_socket
if ($flag == 1){
$flag =0;
send_socket();
}
}
}
close($conf);
#记录错误信息
if($@)
{
my $err_msg=$@;
wrt_log("send message error:".$err_msg,$log_file);
}
阅读(3262) | 评论(0) | 转发(0) |