客户端程序
- hwoyh@S2-E-YZ-WANGGUAN-B:~/perl> cat cli.pl
- #!/usr/bin/perl -w
- use Socket;
- use IO::Handle;
- use strict;
- my($bytes_out,$bytes_in) = (0,0);
- my $host = shift || '10.243.210.67';
- my $port = shift || getservbyname('echo','tcp');
- my $protocol = getprotobyname('tcp');
- $host = inet_aton($host) or die "$host: unknown host";
- socket(SOCK,AF_INET,SOCK_STREAM,$protocol) or die "socket() failed:$!";
- my $dest_addr = sockaddr_in("2007",$host);
- connect(SOCK,$dest_addr) or die "connect() failed: $!";
- print "Enter like: /home/hwoyh/perl/ngcc.pl 15820622410 0120 \n";
- SOCK->autoflush(1);
- my $msg_out = <>;
- print SOCK $msg_out;
- while(defined( my $msg_in =<SOCK>))
- {
- print $msg_in;
- }
- print "Finished..\n";
- close SOCK;
- hwoyh@S2-E-YZ-WANGGUAN-B:~/perl>
服务程序
- hwoyh@S7-E-JK-SZX:/home/hwoyh> cat serv.pl
- #!/usr/bin/perl -w
- use Socket;
- use IO::Handle;
- use strict;
- use constant MY_ECHO_PORT => 2007;
- my($bytes_out,$bytes_in)=(0,0);
- my $port = shift || MY_ECHO_PORT;
- my $protocol = getprotobyname('tcp');
- $SIG{'INT'} = sub {
- exit 0;
- };
- socket(SOCK,AF_INET,SOCK_STREAM,$protocol) or die "socket() failed:$!";
- setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1) or die "can't set SO_REUSEADDR: $!";
- my $my_addr = sockaddr_in($port,INADDR_ANY);
- bind(SOCK,$my_addr) or die "bind() failed:$!";
- listen(SOCK,5) or die "listen() failed:$!";
- warn "waiting for incoming connections on port $port...\n";
- while(1)
- {
- next unless my $remote_addr = accept(SESSION,SOCK);
- my ($port,$hisaddr) = sockaddr_in($remote_addr);
- warn "Connection from [",inet_ntoa($hisaddr),",$port]\n";
- SESSION->autoflush(1);
- while(<SESSION>)
- {
- $bytes_in += length($_);
- chomp;
- my $filename = $1 if(/.+\s+(\d+)\s+\d/);
- #my $output = `$_`;
- #system("/home/hwoyh/perl/ngcc.pl","15820622410","0120");
- open(FILE,"/home/hwoyh/".$filename.".log");
- while(<FILE>)
- {
- print SESSION $_;
- }
- print SESSION 'exit';
- close(FILE);
- }
- warn "Connection from [",inet_ntoa($hisaddr),",$port]\n";
- close(SESSION);
- }
- close(SOCK);
- hwoyh@S7-E-JK-SZX:/home/hwoyh>
远程抓取日志程序
- hwoyh@S7-E-JK-SZX:/home/hwoyh/perl> cat ngcc.pl
- #!/usr/bin/perl
- use Expect;
- if(@ARGV<2)
- {
- print __FILE__." "."\n";
- exit;
- }
- sub load_param
- {
- @list = split (/;|;|=/, shift @_);
- # map(s/\s*(.+)\s*/$1/,@list);
- map(s/^\s+|\s+$//g,@list);
- return @list;
- }
- my $usrname;
- #my $logfile=$servnumber.".log";
- my @ip;
- my $servnumber= shift;
- my $datetime = shift;
- my $exp = new Expect;
- my $timeout = 20;
- my $cmd = "telnet";
- my $region;
- my $path;
- my $password;
- alarm(40);
- $SIG{ALRM} = sub {
- print "\ntime out!\n";
- exit;
- };
- #print "Enter you User Name: ";
- #chomp($usrname=);
- #print "Enter your Password: ";
- #system("stty -echo");
- #chomp($passwd=);
- #print "\n";
- #system("stty echo");
- $SIG{ALRM} = 'IGNORE';
- open(FILE,"/home/hwoyh/perl/config.ini") or die "open file failed:$!";
- while (<FILE>)
- {
- chomp;
- next if(/^#/);
- my @value = &load_param($_);
- if($value[0] eq "ip")
- {
- shift @value;
- @ip = @value;
- }
- $usrname =$value[1] if($value[0] eq "usrname");
- $region = $value[1] if($value[0] eq "region");
- $path = $value[1] if($value[0] eq "path");
- $passwd = $value[1] if($value[0] eq "password");
- }
- print $usrname."\n";
- print $passwd."\n";
- close(FILE);
- #__END__
- print "BEGIN.......\n";
- #$Expect::Log_Stdout = 0;
- foreach $remoteip(@ip)
- {
- chomp;
- print "Searching Host IP: $remoteip...."."\n";
- $exp = Expect->spawn($cmd,$remoteip) or die "Can't spawn $cmd! $!";
- $exp->log_file("/home/hwoyh/".$servnumber.".log");
- $exp->send($usrname."\n") if $exp->expect($timeout,-re=>'[Ll]ogin');
- $exp->send($passwd."\n") if $exp->expect($timeout,-re=>'word');
- if ($exp->expect($timeout,-re=>"${usrname}>"))
- {
- sleep(1);
- $exp->send("zgrep ".$servnumber." ".$path."bosscsc_".$region.".log.".$datetime."*"."\n");
- print "\tDone....\n";
- }
- $exp->send("exit\n") if $exp->expect($timeout,-re=>"${usrname}>");
- $exp->send("\n") if $exp->expect($timeout,-re=>"${usrname}>");
- }
- print "Finish";
边学边做的第一个程序,有待改进,服务程序需要用fork改进,客户端程序需要进一下优化。因为公司系统环境原因只能绕道实现
阅读(1214) | 评论(0) | 转发(0) |