1 |
package Shell::SSH; |
2 |
use strict; |
3 |
use warnings; |
4 |
use base qw( Shell ); |
5 |
our($VERSION, $AUTOLOAD); |
6 |
|
7 |
$VERSION = '0.01'; |
8 |
|
9 |
use Data::Dumper; |
10 |
use IPC::Run qw( start pump finish timeout ); |
11 |
|
12 |
# debugging |
13 |
# $ENV{IPCRUNDEBUG} = 2; |
14 |
|
15 |
# using corkscrew to get through certain http proxies |
16 |
my $corkscrew = '/usr/local/bin/corkscrew'; |
17 |
# good tunnels might be: |
18 |
my $tunnels = [ |
19 |
'proxy.nida.ac.th:8080', # Thailand |
20 |
'210.0.200.211:80', # Hong Kong (ssh1 only) |
21 |
'200.27.152.3:80', # Chile |
22 |
]; |
23 |
|
24 |
local *IN; |
25 |
local *OUT; |
26 |
local *ERR; |
27 |
|
28 |
$Shell::hasHandler = 1; |
29 |
|
30 |
sub new { |
31 |
print "new\n"; |
32 |
my $class = shift; |
33 |
my $self = { @_ }; |
34 |
bless $self, $class; |
35 |
$self->_init(); |
36 |
$self->_start(); |
37 |
return $self; |
38 |
} |
39 |
|
40 |
sub DESTROY { } |
41 |
|
42 |
sub AUTOLOAD { |
43 |
my $self = shift; |
44 |
print "==================AUTO\n"; |
45 |
exit; |
46 |
shift if ref $_[0] && $_[0]->isa( 'Shell' ); |
47 |
#my $cmd = $AUTOLOAD; |
48 |
my $cmd = shift; |
49 |
print "cmd: $cmd\n"; |
50 |
$cmd =~ s/^.*:://; |
51 |
print "===========COMMAND: $cmd", "\n"; |
52 |
} |
53 |
|
54 |
sub _init { |
55 |
print "_init\n"; |
56 |
my $self = shift; |
57 |
|
58 |
#$self->{isHandler} = 1; |
59 |
|
60 |
$self->{options}{proxy_host} = $tunnels->[0] |
61 |
if $self->{options}{proxy_enabled} && !$self->{options}{proxy_host}; |
62 |
|
63 |
$self->{method} ||= 'ssh'; |
64 |
|
65 |
if ($self->{options}{proxy_enabled} && $self->{options}{proxy_host}) { |
66 |
my @proxy = split(/:/, $self->{options}{proxy_host}); |
67 |
$self->{proxy}{host} = $proxy[0]; |
68 |
if ($proxy[1]) { |
69 |
$self->{proxy}{port} = $proxy[1]; |
70 |
} |
71 |
$self->{proxy}{port} ||= 80; |
72 |
} |
73 |
|
74 |
} |
75 |
|
76 |
|
77 |
sub _start { |
78 |
print "_start\n"; |
79 |
my $self = shift; |
80 |
|
81 |
my @cmd = (); |
82 |
die "please supply method" if (!$self->{method}); |
83 |
die "please supply target" if (!$self->{target}); |
84 |
|
85 |
if ($self->{proxy}) { |
86 |
push @cmd, '-o', "ProxyCommand $corkscrew $self->{proxy}{host} $self->{proxy}{port} " . '%h %p'; |
87 |
} |
88 |
|
89 |
push @cmd, $self->{method}; |
90 |
push @cmd, @{$self->{args}} if ($self->{args}); |
91 |
push @cmd, $self->{target} if ($self->{target}); |
92 |
|
93 |
my $cmd = join(" ", @cmd); |
94 |
#print "command: ", $cmd, "\n"; |
95 |
|
96 |
$self->{handle} = start |
97 |
\@cmd, |
98 |
'<pipe', \*IN, |
99 |
'>pipe', \*OUT, |
100 |
'2>pipe', \*ERR, |
101 |
timeout( 3 ) |
102 |
or die "could not open IPC::Run - handle: $?" ; |
103 |
|
104 |
} |
105 |
|
106 |
sub _stop { |
107 |
my $self = shift; |
108 |
finish $self->{handle}; |
109 |
} |
110 |
|
111 |
sub _read_output { |
112 |
my $self = shift; |
113 |
$self->{buffer} = []; |
114 |
while (<OUT>) { |
115 |
chomp(); |
116 |
push @{$self->{buffer}}, $_; |
117 |
} |
118 |
} |
119 |
|
120 |
sub run { |
121 |
my $self = shift; |
122 |
my $rcommand = shift; |
123 |
print IN $rcommand; |
124 |
close IN; |
125 |
$self->_read_output(); |
126 |
} |
127 |
|
128 |
sub get_output { |
129 |
my $self = shift; |
130 |
return $self->{buffer}; |
131 |
} |
132 |
|
133 |
sub disconnect { |
134 |
my $self = shift; |
135 |
$self->_stop(); |
136 |
} |
137 |
|
138 |
1; |