Skip to content

Commit

Permalink
added bin/key2nodes script
Browse files Browse the repository at this point in the history
  • Loading branch information
agentzh committed Apr 21, 2009
1 parent a29ab24 commit f7a39cf
Showing 1 changed file with 257 additions and 0 deletions.
257 changes: 257 additions & 0 deletions bin/key2nodes
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
#!/usr/bin/env perl

use strict;
use warnings;

use lib 'lib';
use Net::OpenSSH;
use Term::ReadKey;
use SSH::Batch::ForNodes;
use File::Temp qw/ :POSIX /;
use File::HomeDir;

sub help ($);

if (!@ARGV) {
warn "No argument specified.\n\n";
help(1);
}

my $list_hosts_only = 0;
my ($user, $port, $timeout, $verbose);
my (@exprs);
my $fetch_value;
for (@ARGV) {
if (defined $fetch_value) {
$fetch_value->($_);
undef $fetch_value;
next;
}
if (/^-([A-Za-z])(.*)/) {
if ($2 ne '') {
die "Unknown option: $_\n";
}
my $group = $1;
if ($group eq 'l') {
$list_hosts_only = 1;
} elsif ($group eq 'u') {
$fetch_value = sub { $user = shift };
} elsif ($group eq 't') {
$fetch_value = sub { $timeout = shift };
} elsif ($group eq 'h') {
help(0);
} elsif ($group eq 'p') {
$fetch_value = sub { $port = shift };
} elsif ($group eq 'v') {
$verbose = 1;
} else {
die "Unknown option: $_\n";
}
next;
}
push @exprs, $_;
}

if (!@exprs) {
die "No cluster expression specified.\n";
}
my $expr = join ' ', @exprs;

if ($verbose) {
warn "Cluster expression: $expr\n";
}

my $home = File::HomeDir->my_home;
if (!defined $home) {
die "Can't find the home for the current user.\n";
}

my $pubkey_file = "$home/.ssh/id_rsa.pub";
if (-f $pubkey_file) {
if ($verbose) {
warn "Found public key file $pubkey_file.\n";
}
} else {
my $cmd = "ssh-keygen -q -t rsa < /dev/null";
if ($verbose) {
warn "Running command [$cmd]...\n";
}
if (system($cmd) != 0) {
die "Generating SSH key failed.\n";
}
}

open my $in, $pubkey_file or
die "Can't open $pubkey_file for reading: $!\n";
my $pubkey = do { local $/; <$in> };
close $in;

my ($rc, $rcfile) = SSH::Batch::ForNodes::init();
SSH::Batch::ForNodes::load_rc($rc, $rcfile);
my $set = SSH::Batch::ForNodes::parse_expr($expr);

if ($set->is_empty) {
die "No machine to be operated.\n";
}
my @hosts = sort $set->elements;

if ($verbose) {
warn "Cluster set: @hosts\n";
} elsif ($list_hosts_only) {
print "Cluster set: @hosts\n";
}

if ($list_hosts_only) {
exit(0);
}

my $password;
print STDERR "Password:";
ReadMode(2);
while (not defined ($password = ReadLine(0))) {
}
ReadMode(0);
chomp $password;
if (!$password) {
die "No password specified.\n";
}

my %conns;
for my $host (@hosts) {
$conns{$host} = Net::OpenSSH->new(
$host,
async => 1,
defined $timeout ? (timeout => $timeout) : (),
defined $user ? (user => $user) : (),
defined $port ? (port => $port) : (),
password => $password,
);
}

my (@pids, @outs);
for my $host (@hosts) {
my ($out, $outfile) = tmpnam();
push @outs, $outfile;
push @pids, $conns{$host}->system({
stdin_data => $pubkey,
stdout_fh => $out,
stderr_to_stdout => 1,
async => 1,
tty => 1,
}, 'cat >> ~/.ssh/authorized_keys && chmod 640 ~/.ssh/authorized_keys');
}

## HERE...
my $i = 0;
for my $pid (@pids) {

my $host = $hosts[$i++];
print "===" x 10, " $host ", "===" x 10, "\n";
if (!defined $pid) {
warn "Failed to connect to host $host.\n";
next;
}
my $exit = 0;
if (waitpid($pid, 0) > 0) {
$exit = ($? >> 8);
} else {
#redo if ($! == EINTR);
warn "$host: waitpid($pid) failed: $!\n";
next;
}
my $outfile = shift @outs;

my $in;
if (!open $in, $outfile) {
warn "Can't open $outfile for reading: $!\n";
next;
}
while (<$in>) {
print;
}
if ($exit > 0) {
warn "Remote command returns status code $exit.\n";
}
print "\n";
close $in;
}

sub help ($) {
my $exit_code = shift;
my $msg = <<'_EOC_';
USAGE:
key2nodes [OPTIONS] HOST_PATTERN... [OPTIONS]
OPTIONS:
-h Print this help.
-l List the hosts and do nothing else.
-p <port> Port for the remote SSH service.
-t <timeout> Specify timeout for net traffic.
-u <user> User account for SSH login.
-v Be verbose.
_EOC_
if ($exit_code == 0) {
print $msg;
exit(0);
} else {
warn $msg;
exit($exit_code);
}
}
__END__
=head1 NAME
key2nodes - Push SSH public keys to remote clusters
=head1 SYNOPSIS
# run a command on the specified servers:
$ key2nodes 'ws[1101-1105].as.com'
=head1 USAGE
key2nodes [OPTIONS] HOST_PATTERN... [OPTIONS]
=head1 OPTIONS
-h Print this help.
-l List the hosts and do nothing else.
-p <port> Port for the remote SSH service.
-t <timeout> Specify timeout for net traffic.
-u <user> User account for SSH login.
-v Be verbose.
=head1 SEE ALSO
L<fornodes>, L<atnodes>, L<tonodes>, L<SSH::Batch>, L<Net::OpenSSH>.
=head1 COPYRIGHT AND LICENSE
This module as well as its programs are licensed under the BSD License.
Copyright (c) 2009, Yahoo! China EEEE Works, Alibaba Inc. All rights reserved.
Copyright (C) 2009, Agent Zhang (agentzh). All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
=over
=item *
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
=item *
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
=item *
Neither the name of the Yahoo! China EEEE Works, Alibaba Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
=back
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 comments on commit f7a39cf

Please sign in to comment.