gco3.pl


Index


One can use "gco" PERL library not only to write PERL PLUGINs to control Proxy32 terminal, but also to:

For those uses one needs third version of the library (gco3.pl) - the one that employs non-blocking I/O when reading from STDIN.

To execute sample PERL PLUGIN in the SCREEN virtual terminal

  1. Download library and sample plugin to user home directory:
  2. Start GNU SCREEN application
  3. Type Ctrl-a then :exec !!. perl plugin.pl in the SCREEN virtual terminal to start the PLUGIN
  4. Plugin will finish automatically and user can continue working in the SCREEN virtual terminal. If PLUGIN is not finishing (for example, expecting wrong pattern), type Ctrl-a then :kill in the SCREEN virtual terminal to kill the PLUGIN. Make sure PLUGIN is running when doing kill, otherwise kill will close current SCREEN window.

To execute sample PERL script that controls spawned bash shell

  1. Download sample PERL script and expect wrapper for bash shell to user home directory:
  2. Type perl interact_with_the_shell.pl at bash prompt to run sample script.

gco3.pl

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
46
47
48
49
##############################################################################################
# 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). 
# 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
# 04/28/13 changed to "non-blocking select/sysread" (gco3.pl) to be able to reuse PLUGINs 
# in the GNU screen as robot filters ("Ctrl-a" ":" "exec !!. perl plugin.pl") 
# *******************************************************************************************
# example how to call:
# gco(\*STDIN,\*STDOUT,\*STDERR,"show sub aaa user $ARGV[0]\r\n","#",0, $buffer);
##############################################################################################

use IO::Select;
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]
    #write command to shell and then do non-blocking non-buffered read till prompt is found
    syswrite $myout,"$commandtoexecute";
    $CommandOutput="";
    my $read_set = new IO::Select();#Create handle set for reading
    $read_set->add($myin);#add the reader handle to the set
    while()
        {
        if ($read_set->can_read(0))
                {
                sysread $myin,$greeting,10000;
                if($showprogress==1){syswrite $myerr,$greeting}
                if($showprogress==2){syswrite $myerr,"#"}
                $CommandOutput.=$greeting;
                if($CommandOutput =~/$promptregex/){last}
                }
        }
    $read_set->remove($myin);#remove the reader handle from the set
    $CommandOutput =~ s/[\r\x00]//g;#clean-up
    $_[0]=$CommandOutput;
}
1;

plugin.pl

1
2
3
4
5
require "gco3.pl";
syswrite STDERR,"\r\n===========PLUGIN is STARTED\r\n";
gco(\*STDIN,\*STDOUT,\*STDERR,"ls -altr\r",'\$',1, $buffer);
syswrite STDERR,"\r\n===========PLUGIN is FINISHED\r\n";
select(undef, undef, undef, 0.25);#let screen some time to print buffers to the user screen

spawn_bash.tcl

1
2
3
spawn -noecho bash
stty raw
interact

interact_with_the_shell.pl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require "gco3.pl";
#########################
#file "spawn_bash.tcl":
#########################
#spawn -noecho bash
#stty raw
#interact
#########################
use IPC::Open3;
use Symbol;
$WTR = gensym();#get reference to typeglog
$RDR = gensym();
$pid = open3($WTR, $RDR, "", 'expect spawn_bash.tcl');#spawn bash shell via expect pty/tty pair


#main program body (can be modified to customize interaction)
gco($RDR,$WTR,\*STDERR,"",'\$',1, $buffer);#read the output of the spawned shell
syswrite STDOUT, "starting sending commands to the shell\r\n";
gco($RDR,$WTR,\*STDERR,"ls -altr\r",'\$',1, $buffer);#send command to the spawned shell
syswrite STDOUT, "\r=======end plugin=======\r";

Index