forked from xcat2/xcat-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteShellExp.pm
932 lines (814 loc) · 27.7 KB
/
RemoteShellExp.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#
package xCAT::RemoteShellExp;
#-----------------------------------------------------------------------------
=head1 RemoteShellExp
Uses perl Expect to set up ssh passwordless login on the input node list
Called from xdsh <nodelist> -K command
It works for node and devices ( such as QLogic Switch).
See man xdsh.
It works for root and non-root userids.
Environment Variables input to drive the setup:
DSH_REMOTE_CMD set to path to remote shell (ssh)
root password must agree on all the nodes
XCAT_ROOT set to root of xCAT install
DSH_REMOTE_PASSWORD - to_user password for -s option required to sendkeys)
Note this is obtained in the xdsh client frontend.
SSH_SETUP_COMMAND - Command to be sent to the IB switch to setup SSH.
DSH_FROM_USERID_HOME - The home directory of the userid from
where the ssh keys will be obtained
to send
DSH_FROM_USERID - The userid from where the ssh keys will be obtained
to send
to the node, or generated and then obtained to send to the
node.
DSH_TO_USERID - The userid on the node where the ssh keys will be updated.
DSH_ENABLE_SSH - Node to node root passwordless ssh will be setup.
DSH_ZONE_SSHKEYS - directory containing the zones root .ssh keys
Usage: remoteshellexp
[-t node list] test ssh connection to the node
[-k] Generates the ssh keys needed , for the user on the MN.
[-s node list] copies the ssh keys to the nodes
optional $timeout = timeout value for the expect. Usually from the xdsh -t flag
default timeout is 10 seconds
exit 0 - good
exit 1 - abort
exit 2 - usage error
Examples:
$rc=xCAT::RemoteShellExp->remoteshellexp("k",$callback,$remoteshellcmd,$nodes,$timeout);
$rc=xCAT::RemoteShellExp->remoteshellexp("s",$callback,$remoteshellcmd,$nodes,$timeout);
$rc=xCAT::RemoteShellExp->remoteshellexp("t",$callback,$remoteshellcmd,$nodes,$timeout);
=cut
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
$::XCATDIR = $ENV{'XCATDIR'} ? $ENV{'XCATDIR'} : '/etc/xcat';
}
use lib "$::XCATROOT/lib/perl";
use xCAT::Utils;
use Getopt::Long;
use xCAT::MsgUtils;
use Expect;
use strict;
#-----------------------------------------------------------------------------
sub remoteshellexp
{
my ($class, $flag, $callback, $remoteshell, $nodes, $timeout) = @_;
my $rc = 0;
$::CALLBACK = $callback;
if (!($flag))
{
my $rsp = {};
$rsp->{error}->[0] =
"No flag provide to remoteshellexp.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 2);
return 2;
}
if (($flag ne "k") && ($flag ne "t") && ($flag ne "s")) {
my $rsp = {};
$rsp->{error}->[0] =
"Invalid flag $flag provided.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 2;
}
my $expecttimeout = 10; # default
if (defined($timeout)) { # value supplied
$expecttimeout = $timeout;
}
# for -s flag must have nodes and a $to_userid password
my $to_user_password;
if ($ENV{'DSH_REMOTE_PASSWORD'}) {
$to_user_password = $ENV{'DSH_REMOTE_PASSWORD'};
}
if ($flag eq "s") {
if (!$to_user_password) {
my $rsp = {};
$rsp->{error}->[0] =
"The DSH_REMOTE_PASSWORD environment variable has not been set to the user id password on the node which will have their ssh keys updated (ususally root).";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 2;
}
if (!$nodes) {
my $rsp = {};
$rsp->{error}->[0] =
"No nodes were input to update the user's ssh keys.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 2;
}
}
my $ssh_setup_cmd;
my $from_userid;
my $to_userid;
my $home;
my $remotecopy;
# if caller input a path to ssh remote command, use it
if ($ENV{'DSH_REMOTE_CMD'}) {
$remoteshell = $ENV{'DSH_REMOTE_CMD'};
} else {
if (!$remoteshell) {
$remoteshell = "/usr/bin/ssh";
}
}
# figure out path to scp
my ($path, $ssh) = split(/ssh/, $remoteshell);
$remotecopy = $path . "scp";
# if caller input the ssh setup command (such as for IB Switch)
if ($ENV{'SSH_SETUP_COMMAND'}) {
$ssh_setup_cmd = $ENV{'SSH_SETUP_COMMAND'};
}
# set User on the Management node that has the ssh keys
# this id can be a local (non-root) id as well as root
if ($ENV{'DSH_FROM_USERID'}) {
$from_userid = $ENV{'DSH_FROM_USERID'};
} else {
$from_userid = "root";
}
# set User on the node where we will send the keys
# this id can be a local id as well as root
if ($ENV{'DSH_TO_USERID'}) {
$to_userid = $ENV{'DSH_TO_USERID'};
} else {
$to_userid = "root";
}
# set User home directory to find the ssh public key to send
# For non-root ids information may not be in /etc/passwd
# but elsewhere like LDAP
if ($ENV{'DSH_FROM_USERID_HOME'}) {
$home = $ENV{'DSH_FROM_USERID_HOME'};
} else {
$home = xCAT::Utils->getHomeDir($from_userid);
}
# This indicates we will generate new ssh keys for the user,
# if they are not already there
# unless using zones
my $key = "$home/.ssh/id_rsa";
my $key2 = "$home/.ssh/id_rsa.pub";
# Check to see if empty
if (-z $key) {
my $rsp = {};
$rsp->{error}->[0] =
"The $key file is empty. Remove it and rerun the command.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 1;
}
if (-z $key2) {
my $rsp = {};
$rsp->{error}->[0] =
"The $key2 file is empty. Remove it and rerun the command.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 1;
}
if (($flag eq "k") && (!(-e $key)))
{
# updating keys and the key file does not exist
$rc = xCAT::RemoteShellExp->gensshkeys($expecttimeout);
}
# send ssh keys to the nodes/devices, to setup passwordless ssh
if ($flag eq "s")
{
if (!($nodes)) {
my $rsp = {};
$rsp->{error}->[0] =
"There are no nodes defined to update the ssh keys.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 1;
}
if ($ssh_setup_cmd) { # setup ssh on devices
$rc = xCAT::RemoteShellExp->senddeviceskeys($remoteshell, $remotecopy, $to_userid, $to_user_password, $home, $ssh_setup_cmd, $nodes, $expecttimeout);
} else { #setup ssh on nodes
if ($ENV{'DSH_ZONE_SSHKEYS'}) { # if using zones the override the location of the keys
$home = $ENV{'DSH_ZONE_SSHKEYS'};
}
$rc = xCAT::RemoteShellExp->sendnodeskeys($remoteshell, $remotecopy, $to_userid, $to_user_password, $home, $nodes, $expecttimeout);
}
}
# test ssh setup on the node
if ($flag eq "t")
{
$rc = xCAT::RemoteShellExp->testkeys($remoteshell, $to_userid, $nodes, $expecttimeout);
}
return $rc;
}
#-----------------------------------------------------------------------------
=head3 gensshkeys
Generates new ssh keys for the input userid on the MN, if they do not
already exist. Test for id_rsa key existence.
=cut
#-----------------------------------------------------------------------------
sub gensshkeys
{
my ($class, $expecttimeout) = @_;
my $keygen;
my $timeout = $expecttimeout; # sets Expect default timeout, 0 accepts immediately
my $keygen_sent = 0;
my $prompt1 = 'Generating public/private rsa';
my $prompt2 = 'Enter file.*:';
my $prompt3 = 'Enter passphrase.*:';
my $prompt4 = 'Enter same passphrase.*:';
my $expect_log = undef;
my $debug = 0;
if ($::VERBOSE)
{
$debug = 1;
}
$keygen = new Expect;
# run /usr/bin/ssh-keygen -t rsa
# prompt1 = 'Generating public/private rsa';
# prompt2 = 'Enter file.*:';
# -re "\r"
# prompt3 = 'Enter passphrase.*:';
# -re "\r"
# prompt4 = 'Enter same passphrase.*:';
# -re "\r"
# disable command echoing
#$keygen->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$keygen->exp_internal(1);
$keygen->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$keygen->log_stdout(1);
$keygen->log_stdout($debug);
# Run the ssh key gen command
my $spawncmd = "/usr/bin/ssh-keygen -t rsa";
unless ($keygen->spawn($spawncmd))
{
my $rsp = {};
$rsp->{error}->[0] =
"Unable to run $spawncmd.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 1;
}
#
#ssh-keygen prompts starts here
#
my @result = $keygen->expect(
$timeout,
[
$prompt1, # Generating public/private rsa
sub {
$keygen->send("\r");
$keygen->clear_accum();
$keygen->exp_continue();
}
],
[
$prompt2, # Enter file.*:
sub {
$keygen->send("\r");
$keygen->clear_accum();
$keygen->exp_continue();
}
],
[
$prompt3, # Enter passphrase.*
sub {
$keygen->send("\r");
$keygen->clear_accum();
$keygen->exp_continue();
}
],
[
$prompt4, # Enter same passphrase.
sub {
$keygen->send("\r");
$keygen->clear_accum();
$keygen->exp_continue();
}
]
); # end prompts
##########################################
# Expect error - report and quit
##########################################
if (defined($result[1]))
{
my $msg = $result[1];
$keygen->soft_close();
if ($msg =~ /status 0/i) { # no error
return 0;
} else {
my $rsp = {};
$rsp->{error}->[0] = $msg;
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
return 1;
}
} else {
$keygen->soft_close();
return 0;
}
}
#-----------------------------------------------------------------------------
=head3 testkeys
Test to see if the remoteshell setup worked
=cut
#-----------------------------------------------------------------------------
sub testkeys
{
my ($class, $remoteshell, $to_userid, $nodes, $expecttimeout) = @_;
my $testkeys;
my $timeout = $expecttimeout; # sets Expect default timeout
my $testkeys_sent = 0;
my $prompt1 = 'Are you sure you want to continue connecting (yes/no)?';
my $prompt2 = 'ssword:';
my $prompt3 = 'Permission denied*';
my $prompt4 = 'test.success';
my $expect_log = undef;
my $debug = 0;
my $rc = 1; # default to error
if ($::VERBOSE)
{
$debug = 1;
}
$testkeys = new Expect;
# run ssh <node> -l to_userid echo test.success
# possible return
# bad
## Are you sure you want to continue connecting (yes/no)?
## *ssword*
## Permission denied.
# Good
## test.success
# disable command echoing
#$testkeys->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$testkeys->exp_internal(1);
$testkeys->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$testkeys->log_stdout(1);
$testkeys->log_stdout($debug);
# Run the ssh key gen command
my $spawncmd = "$remoteshell $nodes -l $to_userid echo test.success";
unless ($testkeys->spawn($spawncmd))
{
my $rsp = {};
$rsp->{error}->[0] =
"Unable to run $spawncmd.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
return 1;
}
#
#testkeys prompts starts here
#
my @result = $testkeys->expect(
$timeout,
[
$prompt1, # Are you sure you want to ...
sub {
$rc = 1;
$testkeys->hard_close();
}
],
[
$prompt2, # *ssword*
sub {
$rc = 1;
$testkeys->hard_close();
}
],
[
$prompt3, # Permission denied
sub {
$rc = 1;
$testkeys->hard_close();
}
],
[
$prompt4, # test.success
sub {
$rc = 0;
}
]
); # end prompts
##########################################
# Expect error - report and quit
##########################################
if (defined($result[1]))
{
my $msg = $result[1];
$testkeys->soft_close();
if ($msg =~ /status 0/i) { # no error
return 0;
} else {
my $rsp = {};
$rsp->{error}->[0] = "Testing the ssh connection to $nodes failed:".$msg;
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
return 1;
}
} else {
$testkeys->soft_close();
return $rc;
}
}
#-------------------------------------------------------------------------------
=head3 sendnodeskeys
Setup the ssh keys on the nodes
=cut
#-----------------------------------------------------------------------------
sub sendnodeskeys
{
my ($class, $remoteshell, $remotecopy, $to_userid, $to_userpassword, $home, $nodes, $expecttimeout) = @_;
my $sendkeys;
my $timeout = $expecttimeout; # sets Expect default timeout, 0 accepts immediately
my $sendkeys_sent = 0;
my $prompt1 = 'Are you sure you want to continue connecting (yes/no)?';
my $prompt2 = 'ssword:';
my $prompt3 = 'Permission denied*';
my $expect_log = undef;
my $debug = 0;
my $rc = 0;
if ($::VERBOSE)
{
$debug = 1;
}
# For each node
# make a temporary directory on the node
# run scp <nodename> -l <to user> /bin/mkdir -p /tmp/$to_userid/.ssh
# xdsh has built an authorized_keys file for the node
# in $HOME/.ssh/tmp/authorized_keys
# copy to the node to the temp directory
# scp $HOME/.ssh/tmp/authorized_keys to_userid@<node>:/tmp/$to_userid/.ssh
# scp $HOME/.ssh/id_rsa.pub to_userid@<node>:/tmp/$to_userid/.ssh
# Note if using zones, the keys do not come from ~/.ssh but from the
# zone table, sshkeydir attribute. For zones the userid is always root
# If you are going to enable ssh to ssh between nodes, then
# scp $HOME/.ssh/id_rsa to that temp directory on the node
# copy the script $HOME/.ssh/copy.sh to the node, it will do the
# the work of setting up the user's ssh keys and clean up
# ssh (run) copy.sh on the node
my @nodelist = split(/,/, $nodes);
foreach my $node (@nodelist) {
$sendkeys = new Expect;
# disable command echoing
#$sendkeys->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$sendkeys->exp_internal(1);
$sendkeys->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$sendkeys->log_stdout(1);
$sendkeys->log_stdout($debug);
# command to make the temp directory on the node
my $spawnmkdir =
"$remoteshell -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null $node -l $to_userid /bin/mkdir -p /tmp/$to_userid/.ssh";
# command to copy the needed files to the node
# send mkdir command
unless ($sendkeys->spawn($spawnmkdir))
{
my $rsp = {};
$rsp->{error}->[0] =
"Unable to run $spawnmkdir on $node";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
next;
}
#
#mkdir prompts starts here
#
my @result = $sendkeys->expect(
$timeout,
[
$prompt1, # Are you sure you want to ...
sub {
$sendkeys->send("yes\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt2, # *ssword*
sub {
$sendkeys->send("$to_userpassword\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt3, # Permission denied
sub {
$rc = 1;
$sendkeys->hard_close();
}
],
); # end prompts
##########################################
# Expect error - report
##########################################
if($rc==1){
my $rsp = {};
$rsp->{error}->[0] = "Permission denied, please make sure the user $to_userid has been created on the node $node and the input password is right\n";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
}
if (defined($result[1]))
{
my $msg = $result[1];
if ($msg =~ /status 0/i) { # no error
$rc = 0;
} else {
if ($msg =~ /2:EOF/i) { # no error
$rc = 0;
} else {
my $rsp = {};
$rsp->{error}->[0] = "Failed to run \"/bin/mkdir -p /tmp/$to_userid/.ssh\" on $node: $msg";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
$rc = 1;
}
}
}
$sendkeys->soft_close();
#
#copy files prompts starts here
#
$sendkeys = new Expect;
# disable command echoing
#$sendkeys->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$sendkeys->exp_internal(1);
$sendkeys->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$sendkeys->log_stdout(1);
$sendkeys->log_stdout($debug);
my $spawncopyfiles;
if ($ENV{'DSH_ENABLE_SSH'}) { # we will enable node to node ssh
$spawncopyfiles =
"$remotecopy -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null $home/.ssh/id_rsa $home/.ssh/id_rsa.pub $home/.ssh/copy.sh $home/.ssh/tmp/authorized_keys $to_userid\@$node:/tmp/$to_userid/.ssh";
} else { # no node to node ssh ( don't send private key)
$spawncopyfiles =
"$remotecopy -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null $home/.ssh/id_rsa.pub $home/.ssh/copy.sh $home/.ssh/tmp/authorized_keys $to_userid\@$node:/tmp/$to_userid/.ssh";
}
# send copy command
unless ($sendkeys->spawn($spawncopyfiles))
{
my $rsp = {};
$rsp->{error}->[0] =
"Unable to run $spawncopyfiles on $node.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
next;
}
@result = $sendkeys->expect(
$timeout,
[
$prompt1, # Are you sure you want to ...
sub {
$sendkeys->send("yes\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt2, # *ssword*
sub {
$sendkeys->send("$to_userpassword\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt3, # Permission denied
sub {
$rc = 1;
$sendkeys->hard_close();
}
],
); # end prompts
##########################################
# Expect error - report
##########################################
if (defined($result[1]))
{
my $msg = $result[1];
if ($msg =~ /status 0/i) { # no error
$rc = 0;
} else {
if ($msg =~ /2:EOF/i) { # no error
$rc = 0;
} else {
my $rsp = {};
$rsp->{error}->[0] = "Failed to copy ssh credentials and helper script to $node: $msg";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
$rc = 1;
}
}
}
$sendkeys->soft_close();
#
# ssh to the node to run the copy.sh to setup the keys starts here
#
$sendkeys = new Expect;
# disable command echoing
#$sendkeys->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$sendkeys->exp_internal(1);
$sendkeys->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$sendkeys->log_stdout(1);
$sendkeys->log_stdout($debug);
# command to run copy.sh
my $spawnruncopy =
"$remoteshell -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null $node -l $to_userid /tmp/$to_userid/.ssh/copy.sh $to_userid";
# send mkdir command
unless ($sendkeys->spawn($spawnruncopy))
{
my $rsp = {};
$rsp->{error}->[0] =
"Unable to run $spawnruncopy.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
next; # go to next node
}
#
#run copy.sh prompts starts here
#
@result = $sendkeys->expect(
$timeout,
[
$prompt1, # Are you sure you want to ...
sub {
$sendkeys->send("yes\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt2, # *ssword*
sub {
$sendkeys->send("$to_userpassword\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt3, # Permission denied
sub {
$rc = 1;
$sendkeys->hard_close();
}
],
); # end prompts
##########################################
# Expect error - report
##########################################
if (defined($result[1]))
{
my $msg = $result[1];
if ($msg =~ /status 0/i) { # no error
$rc = 0;
} else {
if ($msg =~ /2:EOF/i) { # no error
$rc = 0;
} else {
my $rsp = {};
$rsp->{error}->[0] = "Failed to apply the ssh keys on $node:$msg";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
$rc = 1;
}
}
}
$sendkeys->soft_close();
} # end foreach node
return $rc;
}
#-------------------------------------------------------------------------------
=head3 senddeviceskeys
Setup the ssh keys on the switches
=cut
#-----------------------------------------------------------------------------
sub senddeviceskeys
{
my ($class, $remoteshell, $remotecopy, $to_userid, $to_userpassword, $home, $ssh_setup_cmd, $nodes, $expecttimeout) = @_;
my $sendkeys;
my $timeout = $expecttimeout; # sets Expect default timeout, 0 accepts immediately
my $sendkeys_sent = 0;
my $prompt1 = 'Are you sure you want to continue connecting (yes/no)?';
my $prompt2 = 'ssword:';
my $prompt3 = 'Permission denied*';
my $expect_log = undef;
my $debug = 0;
my $rc = 0;
if ($::VERBOSE)
{
$debug = 1;
}
# quote the setup command and key "sshKey add \"<key\""
my $setupcmd = "\"";
$setupcmd .= $ssh_setup_cmd;
$setupcmd .= " ";
# get the public key
my $key = "\\";
$key .= "\"";
$key .= `cat $home/.ssh/tmp/authorized_keys `;
chop($key);
$key .= "\\";
$key .= "\"";
# add to the command
$setupcmd .= $key;
$setupcmd .= "\"";
# Special case for vios
if ($ENV{DEVICETYPE} eq 'vios') {
$setupcmd = "\"echo $key | tee -a ~/.ssh/authorized_keys2\"";
}
# For each input device
my @nodelist = split(/,/, $nodes);
foreach my $node (@nodelist) {
#
# ssh to the node to run the copy.sh to setup the keys starts here
#
$sendkeys = new Expect;
# disable command echoing
#$sendkeys->slave->stty(qw(sane -echo));
#
# exp_internal(1) sets exp_internal debugging
# to STDERR.
#
#$sendkeys->exp_internal(1);
$sendkeys->exp_internal($debug);
#
# log_stdout(0) prevent the program's output from being shown.
# turn on if debugging error
#$sendkeys->log_stdout(1);
$sendkeys->log_stdout($debug);
# command to send key to the device
# sshKey add "key"
my $spawnaddkey =
"$remoteshell $node -l $to_userid $setupcmd";
# send mkdir command
unless ($sendkeys->spawn($spawnaddkey))
{
my $rsp = {};
$rsp->{error}->[0] =
"Unable to run $spawnaddkey.";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK, 1);
next; # go to next node
}
#
#run copy.sh prompts starts here
#
my @result = $sendkeys->expect(
$timeout,
[
$prompt1, # Are you sure you want to ...
sub {
$sendkeys->send("yes\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt2, # *ssword*
sub {
$sendkeys->send("$to_userpassword\r");
$sendkeys->clear_accum();
$sendkeys->exp_continue();
}
],
[
$prompt3, # Permission denied
sub {
$rc = 1;
$sendkeys->soft_close();
next; # go to next node
}
],
); # end prompts
##########################################
# Expect error - report
##########################################
if (defined($result[1]))
{
my $msg = $result[1];
if ($msg =~ /status 0/i) { # no error
$rc = 0;
} else {
my $rsp = {};
$rsp->{error}->[0] = "$node has error,$msg";
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
$rc = 1;
next; # go to next node
}
}
$sendkeys->soft_close();
} # end foreach node
return $rc;
}
1;