gco2.pl

I have run into some types of specialized telecom equipment that have very poorly built command line interpreter. Command line interpreter was running on the same execution thread that does call processing, so the characters of the command should be typed into terminal very slowly, one by one, to minimize a chance the character will be lost by the command interpreter in the remote equipment. To handle such poor design, I had to modify original gco.pl function to send command line to remote host slowly, char by char, waiting 0.5 seconds before sending every next char.

I have commented out line 26 (old way of sending command to remote host) and replaced it by the cycle in lines 27-31 (new way of sending command to remote host). New file is called gco2.pl. To download gco2.pl click here.

To be able to use this function in any PERL plugin simply add following line in the beginning of the plugin code:

require "./plug/perl/gco2.pl";

This line is assuming that . in path to gco2.pl file is referring to Proxy32 startup directory (which is made current directory for any PLUGIN process). Before including this line, please, download file "gco2.pl and place it into subdirectory plug/perl inside of the proxy32 startup directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
##############################################################################################
# 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";
        for (my $key = 0; $key < length($commandtoexecute); $key++){
                syswrite $myerr, substr ($commandtoexecute, $key, 1);
                syswrite $myout, substr ($commandtoexecute, $key, 1);
                select(undef, undef, undef, 0.5);
        }
        $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;