#!/usr/bin/perl # # IP administrator helper. # use 5.005; use IPADM; use IO::Socket; use strict; use vars qw/$sock/; do {print "Usage: helper host port\n"; exit} unless @ARGV == 2; my $neighbourSocket = join ":", $ARGV[0], $ARGV[1]; my $int_count; #Flush back to parent STDOUT->autoflush(1); # unbuffer output $SIG{PIPE} = sub {print "2 Pipe Error.\n$EOF\n"; exit;}; $SIG{TERM} = sub {exit; }; #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; my $sock = IO::Socket::INET->new( PeerAddr => $ARGV[0], Proto => 'tcp', PeerPort => $ARGV[1]); print +((defined $sock) ? "0 Connect OK:$$" : "3 Connect Failed:$$"), "\n$EOF\n"; my $kidpid; # split the program into two processes, identical twins die "can't fork: $!" unless defined($kidpid = fork()); # if parent if ($kidpid) { # parent copies the socket to standard output #Read from parent (STDIN), this is received from TK program via a pipe while() { # if the close server command from TK program is detected, break from loop last if /Close_Server/; } # kill the child process kill("TERM" => $kidpid); } # if child else { # Loop forever until socket is closed (I cant close it!!) while (1) { # Create array to collect output from server my(@data) = (); # Add the addressing information to start of array push @data, "$neighbourSocket\n"; #collect data from daemon until EOF is reached, at this point it can be piped back to TK program while (<$sock>) { push @data, $_; # accumulate command's reply last if /^$EOF$/; } # Catch daemon failure, send output back to TK program for processing print (/^$EOF$/ ? @data : "4 Daemon Failure\n$EOF\n"); } # whilend } close $sock; exit;