############################################################################################## # This subroutine receives command string as a first parameter and sends it to the remote host. # Then it collects the output of the command as one big string ($CommandOutput) # and as array of strings (@CommandOutputLines). End of the command is determined # by the prompt REGEX that is passed as the second argument to the function. # showprogress argument is to control echoing of the output of the command to the terminal # 0 - do not show, 1- verbose, 2 - show '#' sign for every new received chunk of command output # CommandOutput argument is to return output of the command to calling code for post-processing # 04/14/06 Sergey A. Belous # ******************************************************************************************* # 05/02/06 passing file handles correctly into subroutine # 04/18/08 made as separate library for use in inline PERL PLUGINs # ******************************************************************************************* # example how to call: # gco(\*STDIN,\*STDOUT,\*STDERR,"show sub aaa user $ARGV[0]\r\n","#",0, $buffer); ############################################################################################## sub gco{ my $myin = shift; my $myout = shift; my $myerr = shift; my $commandtoexecute = shift; my $promptregex = shift; my $showprogress = shift; #end of argument list, last one is in $_[0] my $greeting=""; syswrite $myout,"$commandtoexecute"; $CommandOutput=""; #@CommandOutputLines = (); while(){ sysread $myin, $greeting,1000; if($showprogress==1){syswrite $myerr,$greeting;} if($showprogress==2){syswrite $myerr,"#";} $CommandOutput.=$greeting;#append new output to the buffer last if $CommandOutput =~ /$promptregex/; } $CommandOutput =~ s/[\r\x00]//g;#clean-up #@CommandOutputLines = split("\n",$CommandOutput);#split into separate lines $_[0]=$CommandOutput; } 1;