- 论坛徽章:
- 0
|
在局域网中有几台机器,现均开tcp端口用于管理,要实现的功能就是“登录相应主机端口,运行一个检查程序,然后根据返回结果来决定是否运行第二个程序”,下面这个脚本虽可以完成这个任务,但是要多次连接主机,也曾尝试使用fork方式来实现,但无法成功。请高手指教,谢谢~~- #!/usr/bin/perl -w
- use IO::Socket;
- my ($ip,$port,$sock,@ans);
- sub connect_to_ip(){
- if(@_<2){
- print "缺少参数: ip port\n";
- return;
- }
- $sock = new IO::Socket::INET (
- PeerAddr => $_[0], #ip
- PeerPort => $_[1], #port
- Proto => 'tcp',
- Timeout => 30,);
- unless($sock){
- die "Error:$!\n";
- return;
- }
- }
- open FILE,"< test.cf" or die "Cannot open file test.cf :$!";
- while(<FILE>){
- next if /^\s*#|^\s*$/;
- ($ip,$port)=($1,$2) if /((?:\d{1,3}.){3}\d{1,3})\s+(\d+)/;
- &connect_to_ip($ip,$port);
- print $sock "check_data\n";
- print $sock "exit\n";
- @ans = <$sock>;
- close($sock);
- foreach $line (@ans){
- chomp;
- if($line =~ /$_.*异常/){
- print $line;
- &connect_to_ip($ip,$port);
- print $sock "restart_proc\n";
- print $sock "exit\n";
- print $out while (defined($out=<$sock>));
- }
- }
- }
- close FILE;
复制代码 |
|