line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Shell::Carapace::SSH; |
2
|
1
|
|
|
1
|
|
1116
|
use Moo; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
264
|
use String::ShellQuote; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
61
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
499
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has callback => (is => 'rw', required => 1); |
8
|
|
|
|
|
|
|
has host => (is => 'rw', required => 1); |
9
|
|
|
|
|
|
|
has ssh_options => (is => 'rw', default => sub { {} }); |
10
|
|
|
|
|
|
|
has ssh => (is => 'rw', lazy => 1, builder => 1, clearer => 1); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _build_ssh { |
13
|
0
|
|
|
0
|
|
|
my $self = shift; |
14
|
0
|
|
|
|
|
|
require Net::OpenSSH; |
15
|
0
|
0
|
|
|
|
|
my %ssh_options = $self->ssh_options ? %{ $self->ssh_options } : (); |
|
0
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
my $ssh = Net::OpenSSH->new($self->host, %ssh_options); |
17
|
0
|
0
|
|
|
|
|
die $ssh->error if $ssh->error; |
18
|
0
|
|
|
|
|
|
return $ssh; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# force ssh builder to run so the connection occurs during object instantiation |
22
|
0
|
|
|
0
|
0
|
|
sub BUILD { shift->ssh } |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub reconnect { |
25
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
26
|
0
|
|
|
|
|
|
$self->clear_ssh; |
27
|
0
|
|
|
|
|
|
$self->ssh; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub run { |
31
|
0
|
|
|
0
|
0
|
|
my ($self, @cmd) = @_; |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
$self->callback->('command', $self->_stringify(@cmd), $self->host); |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
my ($pty, $pid) = $self->ssh->open2pty(@cmd); |
36
|
0
|
0
|
|
|
|
|
die $self->ssh->error if $self->ssh->error; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
while (my $line = <$pty>) { |
39
|
0
|
|
|
|
|
|
$line =~ s/([\r\n])$//g; |
40
|
0
|
|
|
|
|
|
$self->callback->('remote-output', $line, $self->host); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
waitpid($pid, 0); |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
|
if ($? != 0) { |
46
|
0
|
|
|
|
|
|
$self->callback->("error", $self->_stringify(@cmd), $self->host); |
47
|
0
|
|
|
|
|
|
croak "cmd failed"; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _stringify { |
52
|
0
|
|
|
0
|
|
|
my ($self, @cmd) = @_; |
53
|
0
|
0
|
|
|
|
|
return $cmd[0] if @cmd == 1; |
54
|
0
|
|
|
|
|
|
return join(" ", shell_quote @cmd); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |