Andy Baugh 1 сар өмнө
parent
commit
2b7baf43e4

+ 10 - 10
lib/Net/OpenSSH/More.pm

@@ -227,15 +227,15 @@ my $init_ssh = sub {
         # Now, per the POD of Net::OpenSSH, new will NEVER DIE, so just trust it.
         my @base_module_opts =
           qw{host user port password passphrase key_path gateway proxy_command batch_mode ctl_dir ctl_path ssh_cmd scp_cmd rsync_cmd remote_shell timeout kill_ssh_on_timeout strict_mode async connect master_opts default_ssh_opts forward_agent forward_X11 default_stdin_fh default_stdout_fh default_stderr_fh default_stdin_file default_stdout_file default_stderr_file master_stdout_fh master_sdterr_fh master_stdout_discard master_stderr_discard expand_vars vars external_master default_encoding default_stream_encoding default_argument_encoding password_prompt login_handler master_setpgrp master_pty_force};
-	    my $class4super = "Net::OpenSSH::More";
+        my $class4super = "Net::OpenSSH::More";
 
-		# Subclassing here is a bit tricky, especially *after* you have gone down more than one layer.
-		# Ultimately we only ever want the constructor for Net::OpenSSH, so start there and then
-		# Re-bless into subclass if that's relevant.
+        # Subclassing here is a bit tricky, especially *after* you have gone down more than one layer.
+        # Ultimately we only ever want the constructor for Net::OpenSSH, so start there and then
+        # Re-bless into subclass if that's relevant.
         $self = $class4super->SUPER::new( map { $_ => $opts->{$_} } grep { $opts->{$_} } @base_module_opts );
         my $error = $self->error;
         next unless ref $self eq 'Net::OpenSSH::More' && !$error;
-		bless $self, $class if ref $self ne $class;
+        bless $self, $class if ref $self ne $class;
 
         if ( $temp_fh && -s $temp_fh ) {
             seek( $temp_fh, 0, Fcntl::SEEK_SET );
@@ -559,7 +559,7 @@ Dies in this module, as this varies on different platforms (GNU/LINUX, Windows,
 =cut
 
 sub copy {
-	die "Unimplemented, use a subclass of this perhaps?";
+    die "Unimplemented, use a subclass of this perhaps?";
 }
 
 =head2 B<backup_files (FILES)>
@@ -604,8 +604,8 @@ sub backup_files {
                 # then back it up
                 $self->{'file_backups'}{$file} = time;
                 my $bkup = $file . '.' . $self->{'file_backups'}{$file};
-				$self->diag("[INFO] Backing up '$file' to '$bkup'");
-				$self->copy($file, $bkup); # XXX Probably not that portable, maybe move to Linux.pm somehow?
+                $self->diag("[INFO] Backing up '$file' to '$bkup'");
+                $self->copy( $file, $bkup );    # XXX Probably not that portable, maybe move to Linux.pm somehow?
 
                 # otherwise if the file to be backed up doesn't exist
             }
@@ -681,8 +681,8 @@ the parent's destructor kicks in:
 
 sub DESTROY {
     my ($self) = @_;
-    return                                                       if !$self->{'_perl_pid'} || $$ != $self->{'_perl_pid'} || $disable_destructor;
-	$self->restore_files();
+    return if !$self->{'_perl_pid'} || $$ != $self->{'_perl_pid'} || $disable_destructor;
+    $self->restore_files();
     $ENV{SSH_AUTH_SOCK} = $self->{'_opts'}{'_restore_auth_sock'} if $self->{'_opts'}{'_restore_auth_sock'};
     $self->{'persistent_shell'}->close()                         if $self->{'persistent_shell'};
 

+ 16 - 16
lib/Net/OpenSSH/More/Linux.pm

@@ -47,7 +47,15 @@ my $get_addrs_for_iface = sub {
 
 =head3 B<get_primary_adapter>
 
-Method to retrieve the primary device interface from /proc/net/route
+So, on linux, there's no "primary" adapter, just the "correct" adapter
+for whatever given route. As such, what's the best way to determine
+this?
+
+This is a method to guess the "best" device interface from /proc/net/route.
+How does it determine this? By the "metric" stat -- the lower the better,
+as the lower the cost, the higher the preference.
+If you have set the metric improperly, you'll get bad results, but that's
+nothing to do with the code here.
 
 Optionally accepts a truthy arg to indicate whether you want this for the
 local host instead of the remote host.
@@ -57,23 +65,15 @@ local host instead of the remote host.
 sub get_primary_adapter {
     my ( $self, $use_local ) = @_;
     my %interfaces;
-    my $proc_route_path = do {
-        if ($use_local) {
-            File::Slurper::read_text('/proc/net/route');
-        }
-        else {
-            $self->cmd("cat /proc/net/route");
-        }
-    };
+    my $proc_route_path = $use_local ? File::Slurper::read_text('/proc/net/route') : $self->sftp->get_content('/proc/net/route');
     foreach my $line ( split( /\n/, $proc_route_path ) ) {
-        if ( $line =~ m/^(.+?)\s*0{8}\s.*?(\d+)\s+0{8}\s*(?:\d+\s*){3}$/ ) {
-            my ( $interface, $metric ) = ( $1, $2 );
-            push @{ $interfaces{$metric} }, $interface;
-        }
-    }
 
+        #                                        Iface   Destination   Gateway       Flags RefCt Use   Metric  Mask          MTU   Wndow IRTT
+        my ( $interface, $metric ) = $line =~ m/^(.+?)\s+[0-9A-F]{8}\s+[0-9A-F]{8}\s+\d+\s+\d+\s+\d+\s+(\d+)\s+[0-9A-F]{8}\s+\d+\s+\d+\s+\d+\s*$/;
+        push @{ $interfaces{$metric} }, $interface if ( length $interface && defined $metric );
+    }
     my $lowest_metric = ( sort keys %interfaces )[0];
-    my $interface     = $interfaces{$lowest_metric}[0];
+    my $interface     = $interfaces{$lowest_metric}->[0] if defined $lowest_metric && $interfaces{$lowest_metric};
     return $interface || 'eth0';
 }
 
@@ -120,7 +120,7 @@ Effectively the same thing as `cp $SOURCE $DEST` on the remote server.
 =cut
 
 sub copy {
-	my ( $self, $SOURCE, $DEST ) = @_;
+    my ( $self, $SOURCE, $DEST ) = @_;
     return $self->cmd( qw{cp -a}, $SOURCE, $DEST );
 }
 

+ 1 - 1
t/Net-OpenSSH-More-Linux.t

@@ -36,7 +36,7 @@ subtest_streamed "Live tests versus localhost" => sub {
     $obj->backup_files('/tmp/yeehaw');
     $obj->cmd(qw{touch /tmp/yeehaw});
     ok( $obj->sftp->test_e('/tmp/yeehaw'), "Created /tmp/yeehaw touch file for testing backup/restore" );
-    undef $obj;
+    $obj->DESTROY();
     $obj = Net::OpenSSH::More::Linux->new(
         'host' => 'localhost', 'use_persistent_shell' => 0, 'retry_max' => 1,
     );