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