send-config.pl
changeset 7 19ba8f130480
parent 6 e7973168471d
child 8 ea17020436be
equal deleted inserted replaced
6:e7973168471d 7:19ba8f130480
     1 #!/usr/bin/perl -w
     1 #!/usr/bin/perl -w
     2 
     2 
     3 use strict;
     3 use strict;
       
     4 use warnings;
       
     5 
     4 use File::Basename;
     6 use File::Basename;
     5 use Pod::Usage;
     7 use Pod::Usage;
     6 use Getopt::Long;
     8 use Getopt::Long;
     7 use Config::Tiny;
     9 use Config::Tiny;
     8 use Net::SSH qw(sshopen2);
    10 use Net::SSH qw(ssh_cmd);
     9 use Sys::Hostname::Long;
    11 use Sys::Hostname::Long;
    10 use Socket;
    12 use Socket;
    11 use IO::File;
    13 use IO::File;
    12 
    14 
    13 my $ME = basename $0;
    15 my $ME = basename $0;
    60     # adding trailing / to destination directory
    62     # adding trailing / to destination directory
    61     $path .= "/" unless ( $path =~ /\/$/ );
    63     $path .= "/" unless ( $path =~ /\/$/ );
    62     my $dest_path = $path . $source_host;
    64     my $dest_path = $path . $source_host;
    63 
    65 
    64     # create remote destination directory
    66     # create remote destination directory
    65     my $command = "mkdir -m 0700 -p $dest_path";
    67     my $ssh_cmd_args = {
    66 
    68 	    user => $username,
    67     unless ($opt_dry_run) {
    69 	    host => $dest,
    68         sshopen2( "$username\@$dest", *READER, *WRITER, "$command" )
    70 	    command => 'mkdir',
    69           || die "ssh: $!";
    71 	    args => [ '-m 0700', "-p $dest_path" ],
    70     }
    72     };
    71     debug("PROG ssh command: ssh $username\@$dest $command") if $opt_debug;
    73     ssh_cmd $ssh_cmd_args unless $opt_dry_run;
    72     close(READER);
    74 
    73     close(WRITER);
    75     debug("PROG ssh command: ssh $username\@$dest $ssh_cmd_args->{command} ", join ' ', @{$ssh_cmd_args->{args}}) if $opt_debug;
    74 
    76 
    75     rsync( $username, $dest, $dest_path, $stamp );
    77     rsync( $username, $dest, $dest_path, $stamp );
    76 
    78 
    77 }
    79 }
    78 
    80 
   143 
   145 
   144     if ( $pid == 0 ) {
   146     if ( $pid == 0 ) {
   145         open( STDERR, ">&TMP" ) or die "$!";
   147         open( STDERR, ">&TMP" ) or die "$!";
   146         open( STDOUT, ">/dev/null" ) unless $opt_verbose;
   148         open( STDOUT, ">/dev/null" ) unless $opt_verbose;
   147         exec @cmd;
   149         exec @cmd;
       
   150 	warn "Failed to exec '", join ' ', @cmd, "': $!\n";
   148     }
   151     }
   149     else {
   152     else {
   150         waitpid $pid, 0;
   153         waitpid $pid, 0;
   151     }
   154     }
   152 
   155 
   157         open( TMP, "+</tmp/$ME.$$" )
   160         open( TMP, "+</tmp/$ME.$$" )
   158           or die "$ME: Can't open /tmp/$ME.$$";
   161           or die "$ME: Can't open /tmp/$ME.$$";
   159         unlink "/tmp/$ME.$$";
   162         unlink "/tmp/$ME.$$";
   160 
   163 
   161         while (<TMP>) {
   164         while (<TMP>) {
   162             if (/(.*)(?<=failed:)\s+(.*)\s*\(\d+\)/) {
   165 		chomp;
   163                 push @status, "[WARNING] $1 $2"
   166                 push @status, "[WARNING] $_"
   164                   unless ( $opt_keepgoing and !$opt_dry_run );
   167                   unless $opt_keepgoing and !$opt_dry_run;
   165             }
       
   166         }
   168         }
   167         close(TMP);
   169         close(TMP);
   168     }
   170     }
   169 
   171 
   170     if (@status) {
   172     if (@status) {