From a2a2b6da408084b759c949b2cafdadc2188ddce2 Mon Sep 17 00:00:00 2001 From: Michael Sheldon Date: Fri, 13 Jul 2018 07:59:37 -0700 Subject: [PATCH 1/7] Added code for include files --- lib/SSH/Batch/ForNodes.pm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/SSH/Batch/ForNodes.pm b/lib/SSH/Batch/ForNodes.pm index fe69a85..e9f2974 100644 --- a/lib/SSH/Batch/ForNodes.pm +++ b/lib/SSH/Batch/ForNodes.pm @@ -55,10 +55,13 @@ sub init_rc () { sub load_rc ($$) { my ($rc, $rcfile) = @_; my $accum_ln; + while (<$rc>) { + s/\#.*//; next if /^\s*$/; chomp; + if (s/\\\s*$//s) { $accum_ln .= " $_"; next; @@ -68,6 +71,25 @@ sub load_rc ($$) { undef $accum_ln; next; } + if (/^include\s*=\s*"?(~?\/?([\w\.-]+\/)*[\w\.-]+)"?\s*$/i){ + my $includefile = $1; + if($includefile =~ /^~(.*)$/){ + $includefile = ($ENV{SSH_BATCH_HOME} || File::HomeDir->my_home) . $1; + } + + if(! -e $includefile){ + die "Include file $includefile does not exist!\n"; + } + open my $rcsub, $includefile or + die "Can't open $includefile for reading: $!\n"; + + load_rc($rcsub, $includefile); + + close $rcsub; + + next; + } + parse_line($_, $rcfile); } } From 3529e0052e76c9fcb432d873a5296a244300aea4 Mon Sep 17 00:00:00 2001 From: Michael Sheldon Date: Fri, 13 Jul 2018 08:13:34 -0700 Subject: [PATCH 2/7] Moved include logic to parse_line --- lib/SSH/Batch/ForNodes.pm | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/SSH/Batch/ForNodes.pm b/lib/SSH/Batch/ForNodes.pm index e9f2974..fbae280 100644 --- a/lib/SSH/Batch/ForNodes.pm +++ b/lib/SSH/Batch/ForNodes.pm @@ -71,24 +71,6 @@ sub load_rc ($$) { undef $accum_ln; next; } - if (/^include\s*=\s*"?(~?\/?([\w\.-]+\/)*[\w\.-]+)"?\s*$/i){ - my $includefile = $1; - if($includefile =~ /^~(.*)$/){ - $includefile = ($ENV{SSH_BATCH_HOME} || File::HomeDir->my_home) . $1; - } - - if(! -e $includefile){ - die "Include file $includefile does not exist!\n"; - } - open my $rcsub, $includefile or - die "Can't open $includefile for reading: $!\n"; - - load_rc($rcsub, $includefile); - - close $rcsub; - - next; - } parse_line($_, $rcfile); } @@ -103,6 +85,26 @@ sub parse_line ($$) { die "Invalid variable name in $rcfile, line $.: ", "$var\n"; } + if ($var =~ /include/i){ + if($def =~ /\s*"?(~?\/?([\w\.-]+\/)*[\w\.-]+)"?\s*$/){ + my $includefile = $1; + if($includefile =~ /^~(.*)$/){ + $includefile = ($ENV{SSH_BATCH_HOME} || File::HomeDir->my_home) . $1; + } + + if(! -e $includefile){ + die "Include file $includefile does not exist!\n"; + } + open my $rcsub, $includefile or + die "Can't open $includefile for reading: $!\n"; + + load_rc($rcsub, $includefile); + + close $rcsub; + + } + return; + } my $set; eval { $set = parse_expr($def); From f24ab69cfe260b95e69dfa4337c415ec651f8f22 Mon Sep 17 00:00:00 2001 From: Michael Sheldon Date: Fri, 13 Jul 2018 09:43:06 -0700 Subject: [PATCH 3/7] Change include syntax to prevent potential collision with a variable named "include" --- lib/SSH/Batch/ForNodes.pm | 44 ++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/SSH/Batch/ForNodes.pm b/lib/SSH/Batch/ForNodes.pm index fbae280..f458b7c 100644 --- a/lib/SSH/Batch/ForNodes.pm +++ b/lib/SSH/Batch/ForNodes.pm @@ -85,26 +85,6 @@ sub parse_line ($$) { die "Invalid variable name in $rcfile, line $.: ", "$var\n"; } - if ($var =~ /include/i){ - if($def =~ /\s*"?(~?\/?([\w\.-]+\/)*[\w\.-]+)"?\s*$/){ - my $includefile = $1; - if($includefile =~ /^~(.*)$/){ - $includefile = ($ENV{SSH_BATCH_HOME} || File::HomeDir->my_home) . $1; - } - - if(! -e $includefile){ - die "Include file $includefile does not exist!\n"; - } - open my $rcsub, $includefile or - die "Can't open $includefile for reading: $!\n"; - - load_rc($rcsub, $includefile); - - close $rcsub; - - } - return; - } my $set; eval { $set = parse_expr($def); @@ -118,7 +98,29 @@ sub parse_line ($$) { } $Vars{$var} = $set; } - } else { + } + elsif(/^\s*include\s*:\s*(.*)/i){ + if($1 =~ /\s*"?(~?\/?([\w\.-]+\/)*[\w\.-]+)"?\s*$/){ + my $includefile = $1; + if($includefile =~ /^~(.*)$/){ + $includefile = ($ENV{SSH_BATCH_HOME} || File::HomeDir->my_home) . $1; + } + + if(! -e $includefile){ + die "Include file $includefile does not exist!\n"; + } + open my $rcsub, $includefile or + die "Can't open $includefile for reading: $!\n"; + + load_rc($rcsub, $includefile); + + close $rcsub; + + } + return; + + } + else { die "Syntax error in $rcfile, line $.: $_\n"; } } From 983c239008902dd1f8459f7a4cf703b9ce7013c5 Mon Sep 17 00:00:00 2001 From: Michael Sheldon Date: Fri, 13 Jul 2018 10:47:23 -0700 Subject: [PATCH 4/7] Simplify include path handling --- lib/SSH/Batch/ForNodes.pm | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/SSH/Batch/ForNodes.pm b/lib/SSH/Batch/ForNodes.pm index f458b7c..1177db1 100644 --- a/lib/SSH/Batch/ForNodes.pm +++ b/lib/SSH/Batch/ForNodes.pm @@ -99,24 +99,22 @@ sub parse_line ($$) { $Vars{$var} = $set; } } - elsif(/^\s*include\s*:\s*(.*)/i){ - if($1 =~ /\s*"?(~?\/?([\w\.-]+\/)*[\w\.-]+)"?\s*$/){ - my $includefile = $1; - if($includefile =~ /^~(.*)$/){ - $includefile = ($ENV{SSH_BATCH_HOME} || File::HomeDir->my_home) . $1; - } - - if(! -e $includefile){ - die "Include file $includefile does not exist!\n"; - } - open my $rcsub, $includefile or - die "Can't open $includefile for reading: $!\n"; - - load_rc($rcsub, $includefile); - - close $rcsub; - + elsif(/^\s*include\s*:\s*"?([^"]*)"?\s*$/i){ + my $includefile = $1; + if($includefile =~ /^~\/(.*)$/){ + $includefile = ($ENV{SSH_BATCH_HOME} || File::HomeDir->my_home) . '/' . $1; + } + + if(! -e $includefile){ + die "Include file $includefile does not exist!\n"; } + open my $rcsub, $includefile or + die "Can't open $includefile for reading: $!\n"; + + load_rc($rcsub, $includefile); + + close $rcsub; + return; } From a719527e3461b4954f39006b8b7144f8887b0d8b Mon Sep 17 00:00:00 2001 From: Michael Sheldon Date: Fri, 13 Jul 2018 14:41:42 -0700 Subject: [PATCH 5/7] return formatting to original --- lib/SSH/Batch/ForNodes.pm | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/SSH/Batch/ForNodes.pm b/lib/SSH/Batch/ForNodes.pm index 1177db1..11e7233 100644 --- a/lib/SSH/Batch/ForNodes.pm +++ b/lib/SSH/Batch/ForNodes.pm @@ -55,13 +55,10 @@ sub init_rc () { sub load_rc ($$) { my ($rc, $rcfile) = @_; my $accum_ln; - - while (<$rc>) { - + while (<$rc>) { s/\#.*//; next if /^\s*$/; chomp; - if (s/\\\s*$//s) { $accum_ln .= " $_"; next; @@ -71,7 +68,6 @@ sub load_rc ($$) { undef $accum_ln; next; } - parse_line($_, $rcfile); } } From c89d5d56ad41db04f4dedced199acf139e42d18f Mon Sep 17 00:00:00 2001 From: Michael Sheldon Date: Fri, 13 Jul 2018 14:42:22 -0700 Subject: [PATCH 6/7] return formatting to original --- lib/SSH/Batch/ForNodes.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/SSH/Batch/ForNodes.pm b/lib/SSH/Batch/ForNodes.pm index 11e7233..2a950f8 100644 --- a/lib/SSH/Batch/ForNodes.pm +++ b/lib/SSH/Batch/ForNodes.pm @@ -55,7 +55,7 @@ sub init_rc () { sub load_rc ($$) { my ($rc, $rcfile) = @_; my $accum_ln; - while (<$rc>) { + while (<$rc>) { s/\#.*//; next if /^\s*$/; chomp; From 785cd9a89389bddc7ee176486a2ce1240b788fc3 Mon Sep 17 00:00:00 2001 From: Michael Sheldon Date: Sun, 2 Jan 2022 19:37:13 -0700 Subject: [PATCH 7/7] Add ability to choose public key --- bin/key2nodes | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/bin/key2nodes b/bin/key2nodes index 0e6085a..3c4f953 100755 --- a/bin/key2nodes +++ b/bin/key2nodes @@ -23,6 +23,7 @@ my (@exprs); my $concurrency = 20; my $fetch_value; my $ssh_cmd = $ENV{SSH_BATCH_SSH_CMD}; +my $pubkey_file = ""; for (@ARGV) { if (defined $fetch_value) { $fetch_value->($_); @@ -50,6 +51,8 @@ for (@ARGV) { $fetch_value = sub { $ssh_cmd = shift }; } elsif ($group eq 'c') { $fetch_value = sub { $concurrency = shift }; + } elsif ($group eq 'k') { + $fetch_value = sub { $pubkey_file = shift }; } else { die "Unknown option: $_\n"; } @@ -72,12 +75,17 @@ if (!defined $home) { die "Can't find the home for the current user.\n"; } -my $pubkey_file = "$home/.ssh/id_rsa.pub"; +my $genkey = 0; +if ($pubkey_file eq ""){ + $pubkey_file = "$home/.ssh/id_rsa.pub"; + $genkey = 1; +} + if (-f $pubkey_file) { if ($verbose) { warn "Found public key file $pubkey_file.\n"; } -} else { +} elsif ($genkey == 1){ my $cmd = "(echo; echo y; echo; echo) | ssh-keygen -q -t rsa"; if ($verbose) { warn "Running command [$cmd]...\n"; @@ -86,6 +94,9 @@ if (-f $pubkey_file) { die "Generating SSH key failed.\n"; } } +else{ + die "Public key file $pubkey_file not found."; +} open my $in, $pubkey_file or die "Can't open $pubkey_file for reading: $!\n"; @@ -236,6 +247,7 @@ OPTIONS: -c Set SSH concurrency limit. (default: 20) -h Print this help. -l List the hosts and do nothing else. + -k SSH Public Key to upload. -p Port for the remote SSH service. -ssh Specify an alternate ssh program. (This overrides the SSH_BATCH_SSH_CMD environment.) @@ -279,6 +291,7 @@ key2nodes - Push SSH public keys to remote clusters -c Set SSH concurrency limit. (default: 20) -h Print this help. -l List the hosts and do nothing else. + -k SSH Public Key to upload. -p Port for the remote SSH service. -ssh Specify an alternate ssh program. (This overrides the SSH_BATCH_SSH_CMD environment.)