/[cvs]/nfo/perl/scripts/sshwrap/Shell/SSH.pm
ViewVC logotype

Diff of /nfo/perl/scripts/sshwrap/Shell/SSH.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Mon Jan 20 19:01:06 2003 UTC revision 1.2 by joko, Mon Jan 20 19:05:07 2003 UTC
# Line 1  Line 1 
1  package Shell::SSH;  package Shell::SSH;
2  use strict;  use strict;
3  use warnings;  use warnings;
4  use base qw( Shell );  #use base qw( Shell );
5  our($VERSION, $AUTOLOAD);  our($VERSION, $AUTOLOAD);
6    
7  $VERSION = '0.01';  $VERSION = '0.01';
# Line 11  $VERSION = '0.01'; Line 11  $VERSION = '0.01';
11  # we try to imitate "use base qw( Shell 0.4 );" which of course will never work since it "arrays" all values inside qw()  # we try to imitate "use base qw( Shell 0.4 );" which of course will never work since it "arrays" all values inside qw()
12  # but it also calls 'require' under the hood - rendering a "use base ('Shell 0.4');" useless, too, hmmm.....  # but it also calls 'require' under the hood - rendering a "use base ('Shell 0.4');" useless, too, hmmm.....
13  # question: can this already be done in a more formal way?  # question: can this already be done in a more formal way?
14  BEGIN { my $requires = 0.4; die "Shell version $requires required--this is only version $Shell::VERSION" if ($Shell::VERSION lt $requires); }  #BEGIN { my $requires = 0.4; die "Shell version $requires required--this is only version $Shell::VERSION" if ($Shell::VERSION lt $requires); }
15    BEGIN {
16      sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
17    }
18    
19  use Data::Dumper;  use Data::Dumper;
20  use IPC::Run qw( start pump finish timeout );  use IPC::Run qw( start pump finish timeout timer );
21    #use IPC::Run::Win32Helper;
22    
23  # debugging for IPC::Run  # debugging for IPC::Run
24  # $ENV{IPCRUNDEBUG} = 2;  #$ENV{IPCRUNDEBUG} = 10;
25    #$ENV{IPCRUNDEBUG} = 2;
26    $ENV{IPCRUNDEBUG} = 32;
27    
28  local *IN;  local *IN;
29  local *OUT;  local *OUT;
30  local *ERR;  local *ERR;
31    
32  $Shell::hasHandler = 1;  my $in; my $out; my $err; my $handle;
33    my $t;
34    
35  sub new {  sub new {
36    my $class = shift;    my $class = shift;
# Line 37  sub new { Line 44  sub new {
44  sub DESTROY { }  sub DESTROY { }
45    
46  sub AUTOLOAD {  sub AUTOLOAD {
47    my $self = shift if ref $_[0] && $_[0]->isa( 'Shell' );    #my $self = shift if ref $_[0] && $_[0]->isa( 'Shell' );
48      #my $self = shift if ref $_[0] && $_[0]->isa( 'Shell' );
49      my $self = shift;
50    my $cmd = $AUTOLOAD;    my $cmd = $AUTOLOAD;
51    $cmd =~ s/^.*:://;    $cmd =~ s/^.*:://;
52    #print Dumper(@_);    #if ($self) {
   #exit;  
   if ($self) {  
53      # TODO: handle asynchronizity(?) here!      # TODO: handle asynchronizity(?) here!
54      return $self->_run_command($cmd, @_);      return $self->_run_command($cmd, @_);
55    }    #}
56  }  }
57    
58  sub _init {  sub _init {
59    my $self = shift;    my $self = shift;
60      $self->{in}  = '';
61      $self->{out} = '';
62      $self->{err} = '';
63      $in = $out = $err = '';
64    $self->{method} ||= 'ssh';    $self->{method} ||= 'ssh';
65  }  }
66    
# Line 68  sub _start { Line 79  sub _start {
79    my $cmd = join(" ", @cmd);    my $cmd = join(" ", @cmd);
80    #print "command: ", $cmd, "\n";    #print "command: ", $cmd, "\n";
81    
82    $self->{handle} = start  #  $self->{handle} = start
83       \@cmd,  #     \@cmd,
84          '<pipe', \*IN,  #        '<pipe', \*IN,
85          '>pipe', \*OUT,  #        '>pipe', \*OUT,
86          '2>pipe', \*ERR,  #        '2>pipe', \*ERR,
87          timeout( 5 )  #        timeout( 20 )
88       or die "could not open IPC::Run - handle: $?" ;  #     or die "could not open IPC::Run - handle: $?" ;
89      #$self->{handle} = start \@cat, \$in, \$out
90      #$self->{handle} =
91    
92      #print "command: ", join(' ', @cmd), "\n";
93    
94      if (!RUNNING_IN_HELL) {
95        #$t = timeout(15);
96        $t = timer(10);
97        $handle =
98           start(\@cmd, \$in, \$out, \$err, $t)
99           or print "NO HANDLE: $?\n";
100           #or die "could not open IPC::Run - handle: $?" ;
101      } else {
102        $handle = start
103           \@cmd,
104              '<pipe', \*IN,
105              '>pipe', \*OUT,
106              '2>pipe', \*ERR,
107              timeout( 20 )
108           or print "NO HANDLE: $?\n";
109           #or die "could not open IPC::Run - handle: $?" ;
110    
111        # make filehandles hot
112        IN->autoflush(1);
113        OUT->autoflush(1);
114        ERR->autoflush(1);
115      }
116      
117      print "STARTUP FINISHED\n";
118      #sleep 10;
119        
120  }  }
121    
122  sub _stop {  sub _stop {
123    my $self = shift;    my $self = shift;
124    finish $self->{handle};    #finish $self->{handle};
125      #finish $self->{handle};
126      finish $handle;
127  }  }
128    
129  sub _run_command {  sub _run_command {
130    my $self = shift;    my $self = shift;
131    my $rcommand = shift;    my $rcommand = shift;
132    #print "rcmd: $rcommand\n";
133    my $rargs = join(' ', @_);    my $rargs = join(' ', @_);
134    $rargs ||= '';    $rargs ||= '';
135    my $rcommandstring = join(' ', $rcommand, $rargs);    my $rcommandstring = join(' ', $rcommand, $rargs);
136      $rcommandstring =~ s/\s//g;
137      #print "cmd: '", $rcommandstring, "'\n";
138    #open IN, '';    #open IN, '';
139    print IN $rcommandstring;    #$self->_read_output();
140    close IN;    #print IN $rcommandstring;
141    $self->_read_output();    #close IN;
142      #pump $self->{handle};
143      #$self->{in} = $rcommandstring;
144    
145      print "COMMAND SENT: $rcommandstring\n";
146    
147      if (!RUNNING_IN_HELL) {
148        $in = $rcommandstring . "\n";
149      } else {
150        print IN $rcommandstring, "\n";
151        #close IN;
152      }
153      #$in = $rcommandstring;
154      #pump $self->{handle};
155      #$out = '';
156      #pump $handle while length $in;
157      pump $handle;
158      #pump $handle;
159    
160      #print "first pumping ready", "\n";
161    
162      #$self->_read_output();
163    
164    return '';    return '';
165  }  }
166    
167  sub _read_output {  sub _read_output {
168      #print "_read_output\n";
169    my $self = shift;    my $self = shift;
170    $self->{buffer} = [];    $self->{buffer} = [];
171    my $buffer = '';    my $buffer = '';
172    #print "=read\n";    #print "=read\n";
173    
174    #print Dumper($self->{handle});    #print Dumper($self->{handle});
175    while (<OUT>) {    #while (<OUT>) {
176      #print "line: ", $_, "\n";    #pump $self->{handle};
177      chomp();    #sleep 1; pump $self->{handle};
178      push @{$self->{buffer}}, $_;    #sleep 1; pump $self->{handle};
179      $buffer .= $_ . "\n";    #pump $self->{handle};
180      #pump $handle;
181    
182      #pump $handle while length $out;
183      #pump $handle;
184      #pump $handle;
185      #pump $handle;
186      #pump $handle until length $out;
187      #$self->{response}->($out) if $out;
188      #return;
189    
190    #print Dumper($self->{handle});
191    
192      # the command-response-roundtrip should
193      # take a smaller amount of time than the initial connection
194      my $tcmd = timer(5);
195    
196      until ($tcmd->is_expired) {
197    
198        if (!RUNNING_IN_HELL) {
199      
200          pump $handle;
201          #while (!$t->is_expired) {
202          while ($out && !$tcmd->is_expired) {
203            print ".\n";
204            chomp($out);
205            print "line: ", $out, "\n";
206            $self->{response}($out) if $out;
207            #push @{$self->{buffer}}, $_;
208            $buffer .= $out . "\n";
209            #pump $self->{handle};
210            pump $handle;
211          }
212      
213        } else {
214      
215          print "=========WIN32\n";
216          
217          #pump $handle;
218          #until (my $line = <OUT> || $tcmd->is_expired) {
219          my $line = <OUT>;
220          chomp($line);
221          print "line: ", $line, "\n";
222          $buffer .= $out . "\n";
223          #pump $self->{handle};
224          pump $handle;
225      
226        }
227    
228    }    }
229    #print "=read okay\n";  
230      print "=read okay\n";
231      
232      if ($tcmd->is_expired) {
233        print "command expired", "\n";
234      }
235      
236    # TODO: async!    # TODO: async!
237    #return $buffer;    #return $buffer;
238    $self->{response}->($buffer);    $self->{response}->($buffer) if $buffer;
239  }  }
240    
241  sub get_output {  sub get_output {

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed