| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package App::PersistentSSH; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use MooseX::POE; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use POE::Wheel::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = "0.04"; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | with qw( | |||||||||||||||||||||||||||
| 13 | MooseX::Getopt | ||||||||||||||||||||||||||||
| 14 | MooseX::LogDispatch | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | has host => ( | |||||||||||||||||||||||||||
| 18 | isa => "Str", | ||||||||||||||||||||||||||||
| 19 | is => "rw", | ||||||||||||||||||||||||||||
| 20 | required => 1, | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | has ssh_verbose => ( | |||||||||||||||||||||||||||
| 24 | isa => "Bool", | ||||||||||||||||||||||||||||
| 25 | is => "rw", | ||||||||||||||||||||||||||||
| 26 | default => 0, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | has ssh => ( | |||||||||||||||||||||||||||
| 30 | isa => "Str", | ||||||||||||||||||||||||||||
| 31 | is => "rw", | ||||||||||||||||||||||||||||
| 32 | default => "ssh", | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | has ssh_master_opts => ( | ||||||||||||||||||||||||||||
| 36 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 37 | is => "rw", | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | default => sub { [qw(-o ControlMaster=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -N)] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 1 | ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has ssh_opts => ( | ||||||||||||||||||||||||||||
| 42 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 43 | is => "rw", | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 1 | ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | has scutil => ( | |||||||||||||||||||||||||||
| 48 | isa => "Str", | ||||||||||||||||||||||||||||
| 49 | is => "rw", | ||||||||||||||||||||||||||||
| 50 | default => "scutil", | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | has ipconfig => ( | |||||||||||||||||||||||||||
| 54 | isa => "Str", | ||||||||||||||||||||||||||||
| 55 | is => "rw", | ||||||||||||||||||||||||||||
| 56 | default => "ipconfig", | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | has _stopping_ssh => ( | |||||||||||||||||||||||||||
| 60 | isa => "Bool", | ||||||||||||||||||||||||||||
| 61 | is => "rw", | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | has _ssh_wheel => ( | |||||||||||||||||||||||||||
| 65 | isa => "POE::Wheel::Run", | ||||||||||||||||||||||||||||
| 66 | is => "rw", | ||||||||||||||||||||||||||||
| 67 | predicate => "_has_ssh_wheel", | ||||||||||||||||||||||||||||
| 68 | clearer => "_clear_ssh_wheel", | ||||||||||||||||||||||||||||
| 69 | handles => { | ||||||||||||||||||||||||||||
| 70 | _ssh_pid => "PID", | ||||||||||||||||||||||||||||
| 71 | _kill_ssh => "kill", | ||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | has _scutil_wheel => ( | |||||||||||||||||||||||||||
| 76 | isa => "POE::Wheel::Run", | ||||||||||||||||||||||||||||
| 77 | is => "rw", | ||||||||||||||||||||||||||||
| 78 | predicate => "_has_scutil_wheel", | ||||||||||||||||||||||||||||
| 79 | clearer => "_clear_scutil_wheel", | ||||||||||||||||||||||||||||
| 80 | handles => { _scutil_pid => "PID" }, | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub START { | ||||||||||||||||||||||||||
|
Subroutine
START: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 0 | my ( $self, $kernel ) = @_[OBJECT, KERNEL]; | |||||||||||||||||||||||||||
| 85 | 0 | $kernel->yield("start_scutil"); | |||||||||||||||||||||||||||
| 86 | 0 | $kernel->yield("try_spawn"); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | event network_changed => sub { | ||||||||||||||||||||||||||
|
Subroutine
network_changed: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 90 | 0 | my ( $self, $kernel ) = @_[OBJECT, KERNEL]; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | $self->logger->info("network state changed"); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | $kernel->yield("try_spawn"); | |||||||||||||||||||||||||||
| 95 | 1 | }; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | event try_spawn => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 98 | 0 | my ( $self, $kernel ) = @_[OBJECT, KERNEL]; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ( $self->is_reachable ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $kernel->yield("start_ssh"); | |||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | 0 | $kernel->yield("stop_ssh"); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 1 | }; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub is_reachable { | ||||||||||||||||||||||||||
|
Subroutine
is_reachable: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 0 | my ( $self, $host ) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $host ||= $self->host; | ||||||||||||||||||||||||||
|
Condition0%0/3
$host ||= $self->host
MC/DC0%0/2
$host ||= $self->host
$host
$self->host
Truth table0%0/3
A ||= B
A= $hostB= $self->host
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # wait for the network interfaces to be configured | ||||||||||||||||||||||||||||
| 113 | 0 | $self->logger->debug("ipconfig wait all"); | |||||||||||||||||||||||||||
| 114 | 0 | system( $self->ipconfig, "waitall" ); | |||||||||||||||||||||||||||
| 115 | 0 | $self->logger->info("ipconfig waitall reports interface is configured"); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # check for reachability | ||||||||||||||||||||||||||||
| 118 | 0 | my $scutil = $self->scutil; | |||||||||||||||||||||||||||
| 119 | 0 | my $out = `$scutil -r $host`; | |||||||||||||||||||||||||||
| 120 | 0 | chomp $out; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | $self->logger->debug("scutil -r $host: $out"); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ( $out =~ /^Reachable/ and not $out =~ /Connection (?:Required|Automatic)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$out =~ /^Reachable/ and not $out =~ /Connection (?:Required|Automatic)/
MC/DC0%0/2
$out =~ /^Reachable/ and not $out =~ /Connection (?:Required|Automatic)/
$out =~ /^Reachable/
not $out =~ /Connection (?:Required|Automatic)/
Truth table0%0/3
A and not(B)
A= $out =~ /^Reachable/B= not $out =~ /Connection (?:Required|Automatic)/
| |||||||||||||||||||||||||||||
| 125 | 0 | $self->logger->debug("$host reachable"); | |||||||||||||||||||||||||||
| 126 | 0 | return 1; | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 0 | $self->logger->info("$host not reachable"); | |||||||||||||||||||||||||||
| 129 | 0 | return 0; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub is_running { | ||||||||||||||||||||||||||
|
Subroutine
is_running: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | return unless $self->_has_ssh_wheel; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | kill 0 => $self->_ssh_pid; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub create_ssh_args { | ||||||||||||||||||||||||||
|
Subroutine
create_ssh_args: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | return [ ( $self->ssh_verbose ? "-v" : () ), @{ $self->ssh_master_opts }, @{ $self->ssh_opts }, $self->host ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | event spawn_command => sub { | ||||||||||||||||||||||||||
|
Subroutine
spawn_command: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 145 | 0 | my ( $self, $kernel, $command, @args ) = @_[OBJECT, KERNEL, ARG0, ARG1 .. $#_]; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | my ( $program, $args ) = ( | ||||||||||||||||||||||||||||
| 148 | $self->$command, | ||||||||||||||||||||||||||||
| 149 | 0 | $self->${\"create_${command}_args"}, | |||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | $self->logger->info("spawning", join(" ", $program, @$args)); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | my $wheel = POE::Wheel::Run->new( | ||||||||||||||||||||||||||||
| 155 | Program => $program, | ||||||||||||||||||||||||||||
| 156 | ProgramArgs => $args, | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ( map { ucfirst() . 'Event' => "${command}_$_" } qw( | |||||||||||||||||||||||||||
| 159 | stdin | ||||||||||||||||||||||||||||
| 160 | stdout | ||||||||||||||||||||||||||||
| 161 | stderr | ||||||||||||||||||||||||||||
| 162 | error | ||||||||||||||||||||||||||||
| 163 | close | ||||||||||||||||||||||||||||
| 164 | )), | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ( map { $_ . Filter => POE::Filter::Line->new } qw(Stdout Stderr Stdin) ), | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | @args, | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | $kernel->sig_child( $wheel->PID, "${command}_died" ); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | $self->${\"_${command}_wheel"}($wheel); | |||||||||||||||||||||||||||
| 174 | 1 | }; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | event start_ssh => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 177 | 0 | my ( $self, $kernel ) = @_[OBJECT, KERNEL]; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | unless ( $self->is_running ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | $self->call(spawn_command => "ssh"); | |||||||||||||||||||||||||||
| 181 | 0 | $kernel->sig( INT => "stop_ssh" ); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 1 | }; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | event ssh_stderr => sub { | ||||||||||||||||||||||||||
|
Subroutine
ssh_stderr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 186 | 0 | $_[OBJECT]->logger->warning(@_[ARG0 .. $#_]); | |||||||||||||||||||||||||||
| 187 | 1 | }; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | event ssh_died => sub { | ||||||||||||||||||||||||||
|
Subroutine
ssh_died: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 190 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | $self->_clear_ssh_wheel; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if ( $self->_stopping_ssh ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | $self->_stopping_ssh(0); | |||||||||||||||||||||||||||
| 196 | 0 | $self->logger->info("ssh stopped"); | |||||||||||||||||||||||||||
| 197 | } else { | ||||||||||||||||||||||||||||
| 198 | 0 | $self->logger->warning("ssh died") | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | $self->yield("try_spawn"); | |||||||||||||||||||||||||||
| 202 | 1 | }; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | event stop_ssh => sub { | ||||||||||||||||||||||||||
|
Subroutine
stop_ssh: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 205 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | if ( $self->_has_ssh_wheel ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | $self->_stopping_ssh(1); | |||||||||||||||||||||||||||
| 209 | 0 | $self->logger->info("stopping ssh"); | |||||||||||||||||||||||||||
| 210 | 0 | $self->_kill_ssh; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 1 | }; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | sub create_scutil_args { | ||||||||||||||||||||||||||
|
Subroutine
create_scutil_args: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 215 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 216 | 0 | return [ ]; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | ▶ | event start_scutil => sub { | ||||||||||||||||||||||||||
|
Subroutine
start_scutil: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 220 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | $self->call( spawn_command => "scutil" ); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | $self->_scutil_wheel->put( | |||||||||||||||||||||||||||
| 225 | "n.add State:/Network/Global/IPv4", | ||||||||||||||||||||||||||||
| 226 | "n.watch" | ||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||
| 228 | 1 | }; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | ▶ | event scutil_died => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 231 | 0 | my $self = $_[OBJECT]; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | $self->logger->warning("scutil died"); | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | $self->_clear_scutil_wheel; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | $self->yield("start_scutil"); | |||||||||||||||||||||||||||
| 238 | 1 | }; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | ▶ | event scutil_stderr => sub { | ||||||||||||||||||||||||||
|
Subroutine
scutil_stderr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 241 | 0 | my ( $self, $kernel, $output ) = @_[OBJECT, KERNEL, ARG0]; | |||||||||||||||||||||||||||
| 242 | 0 | $self->logger->debug("scutil err: $output"); | |||||||||||||||||||||||||||
| 243 | 1 | }; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | event scutil_stdout => sub { | ||||||||||||||||||||||||||
|
Subroutine
scutil_stdout: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 246 | 0 | my ( $self, $kernel, $output ) = @_[OBJECT, KERNEL, ARG0]; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if ( $output =~ m{^\s*changed key \[\d+\] = State:/Network/Global/IPv4} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 249 | 0 | $kernel->yield("network_changed"); | |||||||||||||||||||||||||||
| 250 | } elsif ( $output !~ m{^\s*notification callback} ) { | ||||||||||||||||||||||||||||
| 251 | 0 | $self->logger->debug("scutil out: $output"); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | 1 | }; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 256 | 0 | POE::Kernel->run; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 1 | __PACKAGE__ | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | __END__ | ||||||||||||||||||||||||||||