2012-11-13 22:03:57 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# IBM(c) 2012 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
package xCAT::RShellAPI;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
{
|
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
|
|
|
|
}
|
|
|
|
use lib "$::XCATROOT/lib/perl";
|
|
|
|
use xCAT::MsgUtils;
|
2012-11-29 21:52:15 +00:00
|
|
|
#use Data::Dumper;
|
2012-11-13 22:03:57 +00:00
|
|
|
|
|
|
|
#######################################################
|
|
|
|
=head3
|
|
|
|
remote_shell_command
|
|
|
|
|
|
|
|
This routine constructs an remote shell command using the
|
|
|
|
given arguments
|
|
|
|
Arguments:
|
|
|
|
$class - Calling module name (discarded)
|
|
|
|
$config - Reference to remote shell command configuration hash table
|
|
|
|
$exec_path - Path to ssh executable
|
|
|
|
Returns:
|
|
|
|
A command array for the ssh command with the appropriate
|
|
|
|
arguments as defined in the $config hash table
|
|
|
|
=cut
|
2012-11-16 17:21:35 +00:00
|
|
|
#####################################################
|
2012-11-13 22:03:57 +00:00
|
|
|
sub remote_shell_command {
|
|
|
|
my ( $class, $config, $exec_path ) = @_;
|
2012-12-05 20:39:43 +00:00
|
|
|
#print Dumper($config);
|
2012-11-13 22:03:57 +00:00
|
|
|
|
|
|
|
my @command = ();
|
|
|
|
|
|
|
|
push @command, $exec_path;
|
|
|
|
|
|
|
|
if ( $$config{'options'} ) {
|
|
|
|
my @options = split ' ', $$config{'options'};
|
|
|
|
push @command, @options;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @tmp;
|
2012-11-29 21:52:15 +00:00
|
|
|
if ( $$config{'trace'} ) {
|
|
|
|
push @command, "-v";
|
|
|
|
}
|
2012-12-06 21:11:29 +00:00
|
|
|
if ( $$config{'remotecmdproto'} && ($$config{'remotecmdproto'} =~ /^telnet$/)) {
|
|
|
|
push @command, "-t";
|
|
|
|
}
|
2012-11-13 22:03:57 +00:00
|
|
|
if ($$config{'user'} && ($$config{'user'} !~ /^none$/i)) {
|
2012-12-06 21:11:29 +00:00
|
|
|
@tmp=split(' ', "-l $$config{'user'}");
|
|
|
|
push @command, @tmp;
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
if ($$config{'password'} && ($$config{'password'} !~ /^none$/i)) {
|
|
|
|
@tmp=split(' ', "-p $$config{'password'}");
|
|
|
|
push @command, @tmp;
|
|
|
|
}
|
|
|
|
push @command, "$$config{'hostname'}";
|
|
|
|
push @command, $$config{'command'};
|
|
|
|
|
|
|
|
return @command;
|
|
|
|
}
|
|
|
|
|
2012-11-16 17:21:35 +00:00
|
|
|
##################################################################
|
|
|
|
=head3
|
|
|
|
run_remote_shell_api
|
|
|
|
|
|
|
|
This routine tried ssh then telnet to logon to a node and
|
|
|
|
run a sequence of commands.
|
|
|
|
Arguments:
|
|
|
|
$node - node name
|
|
|
|
$user - user login name
|
|
|
|
$passed - user login password
|
|
|
|
$cmds - a list of commands seperated by semicolon.
|
|
|
|
Returns:
|
|
|
|
[error code, output]
|
|
|
|
error code: 0 sucess
|
|
|
|
non-zero: failed. the output contains the error message.
|
|
|
|
=cut
|
|
|
|
#########################################################################
|
2012-11-13 22:03:57 +00:00
|
|
|
sub run_remote_shell_api {
|
|
|
|
require xCAT::SSHInteract;
|
|
|
|
my $node=shift;
|
|
|
|
my $user=shift;
|
|
|
|
my $passwd=shift;
|
2012-12-05 20:39:43 +00:00
|
|
|
my $telnet=shift;
|
2012-11-29 21:52:15 +00:00
|
|
|
my $verbose=shift;
|
2012-11-13 22:03:57 +00:00
|
|
|
my $args = join(" ", @_);
|
|
|
|
my $t;
|
2012-11-29 21:52:15 +00:00
|
|
|
my $prompt='.*[\>\#\$]\s*$';
|
|
|
|
my $more_prompt='(.*key to continue.*|.*--More--\s*|.*--\(more.*\)--.*$)';
|
2012-11-16 17:21:35 +00:00
|
|
|
my $output;
|
2012-12-05 20:39:43 +00:00
|
|
|
my $errmsg;
|
2012-12-06 21:11:29 +00:00
|
|
|
my $ssh_tried=0;
|
2013-09-10 18:23:22 +00:00
|
|
|
my $nl_tried=0;
|
2012-11-13 22:03:57 +00:00
|
|
|
|
2012-12-05 20:39:43 +00:00
|
|
|
if (!$telnet) {
|
|
|
|
eval {
|
|
|
|
$output="start SSH session...\n";
|
2012-12-06 21:11:29 +00:00
|
|
|
$ssh_tried=1;
|
2012-12-05 20:39:43 +00:00
|
|
|
$t = new xCAT::SSHInteract(
|
|
|
|
-username=>$user,
|
|
|
|
-password=>$passwd,
|
|
|
|
-host=>$node,
|
|
|
|
-nokeycheck=>1,
|
|
|
|
-output_record_separator=>"\r",
|
|
|
|
Timeout=>10,
|
|
|
|
Errmode=>'return',
|
|
|
|
Prompt=>"/$prompt/",
|
|
|
|
);
|
|
|
|
};
|
|
|
|
$errmsg=$@;
|
|
|
|
$errmsg =~ s/ at (.*) line (\d)+//g;
|
|
|
|
$output.="$errmsg\n";
|
|
|
|
}
|
2012-11-13 22:03:57 +00:00
|
|
|
|
|
|
|
my $rc=1;
|
2012-11-29 21:52:15 +00:00
|
|
|
if ($t) {
|
|
|
|
#Wait for command prompt
|
|
|
|
my ($prematch, $match) = $t->waitfor(Match => '/login[: ]*$/i',
|
|
|
|
Match => '/username[: ]*$/i',
|
|
|
|
Match => '/password[: ]*$/i',
|
|
|
|
Match => "/$prompt/",
|
|
|
|
Errmode => "return");
|
|
|
|
if ($verbose) {
|
|
|
|
print "0. prematch=$prematch\n match=$match\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($match !~ /$prompt/) {
|
|
|
|
return [1, $output];
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
#ssh failed.. fallback to a telnet attempt
|
2012-12-06 21:11:29 +00:00
|
|
|
if ($ssh_tried) {
|
|
|
|
$output.="Warning: SSH failed, will try Telnet. Please set switches.protocol=telnet next time if you wish to use telnet directly.\n";
|
|
|
|
}
|
2012-11-16 17:21:35 +00:00
|
|
|
$output.="start Telnet session...\n";
|
2012-11-13 22:03:57 +00:00
|
|
|
require Net::Telnet;
|
|
|
|
$t = new Net::Telnet(
|
2012-11-28 16:32:36 +00:00
|
|
|
Timeout=>10,
|
2012-11-13 22:03:57 +00:00
|
|
|
Errmode=>'return',
|
2012-11-16 17:21:35 +00:00
|
|
|
Prompt=>"/$prompt/",
|
2012-11-13 22:03:57 +00:00
|
|
|
);
|
|
|
|
$rc = $t->open($node);
|
|
|
|
if ($rc) {
|
|
|
|
my $pw_tried=0;
|
2012-11-16 17:21:35 +00:00
|
|
|
my $login_done=0;
|
2012-11-13 22:03:57 +00:00
|
|
|
my ($prematch, $match)= $t->waitfor(Match => '/login[: ]*$/i',
|
|
|
|
Match => '/username[: ]*$/i',
|
|
|
|
Match => '/password[: ]*$/i',
|
2012-11-16 17:21:35 +00:00
|
|
|
Match => "/$prompt/",
|
2012-11-13 22:03:57 +00:00
|
|
|
Errmode => "return");
|
2012-11-16 17:21:35 +00:00
|
|
|
if ($verbose) {
|
|
|
|
print "1. prematch=$prematch\n match=$match\n";
|
|
|
|
}
|
|
|
|
if ($match =~ /$prompt/) {
|
|
|
|
$login_done=1;
|
|
|
|
} elsif (($match =~ /username[: ]*$/i) || ($match =~ /login[: ]*$/i )) {
|
2012-11-13 22:03:57 +00:00
|
|
|
# user name
|
|
|
|
if ($user) {
|
|
|
|
if (! $t->put(String => "$user\n",
|
|
|
|
Errmode => "return")) {
|
2012-11-16 17:21:35 +00:00
|
|
|
$output.="login disconnected\n";
|
|
|
|
return [1, $output];
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-11-16 17:21:35 +00:00
|
|
|
$output.="Username is required.\n";
|
|
|
|
return [1, $output];
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
} elsif ($match =~ /password[: ]*$/i) {
|
|
|
|
if ($passwd) {
|
|
|
|
$pw_tried=1;
|
|
|
|
if (! $t->put(String => "$passwd\n",
|
|
|
|
Errmode => "return")) {
|
2012-11-16 17:21:35 +00:00
|
|
|
$output.="Login disconnected\n";
|
|
|
|
return [1, $output];
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-11-16 17:21:35 +00:00
|
|
|
$output.="Password is required.\n";
|
|
|
|
return [1, $output];
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-16 17:21:35 +00:00
|
|
|
|
|
|
|
if (!$login_done) {
|
|
|
|
($prematch, $match)= $t->waitfor(Match => '/login[: ]*$/i',
|
2012-11-13 22:03:57 +00:00
|
|
|
Match => '/username[: ]*$/i',
|
|
|
|
Match => '/password[: ]*$/i',
|
2012-11-16 17:21:35 +00:00
|
|
|
Match => "/$prompt/",
|
2012-11-13 22:03:57 +00:00
|
|
|
Errmode => "return");
|
|
|
|
|
2012-11-16 17:21:35 +00:00
|
|
|
if ($verbose) {
|
|
|
|
print "2. prematch=$prematch\n match=$match\n";
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
2012-11-16 17:21:35 +00:00
|
|
|
if ($match =~ /$prompt/) {
|
|
|
|
$login_done=1;
|
|
|
|
} elsif (($match =~ /username[: ]*$/i) || ($match =~ /login[: ]*$/i )) {
|
|
|
|
$output.="Incorrect username.\n";
|
|
|
|
return [1, $output];
|
|
|
|
} elsif ($match =~ /password[: ]*$/i) {
|
|
|
|
if ($pw_tried) {
|
|
|
|
$output.="Incorrect password.\n";
|
|
|
|
return [1, $output];
|
|
|
|
}
|
|
|
|
if ($passwd) {
|
|
|
|
if (! $t->put(String => "$passwd\n",
|
|
|
|
Errmode => "return")) {
|
|
|
|
$output.="Login disconnected\n";
|
|
|
|
return [1, $output];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$output.="Password is required.\n";
|
|
|
|
return [1, $output];
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
2013-09-10 18:23:22 +00:00
|
|
|
} else {
|
|
|
|
# for some switches like BNT, user has to type an extra new line
|
|
|
|
# in order to get the prompt.
|
|
|
|
if ($verbose) {
|
|
|
|
print " add a newline\n";
|
|
|
|
}
|
|
|
|
$nl_tried=1;
|
|
|
|
if (! $t->put(String => "\n",
|
|
|
|
Errmode => "return")) {
|
|
|
|
$output.="Login disconnected\n";
|
|
|
|
return [1, $output];
|
|
|
|
}
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 21:52:15 +00:00
|
|
|
if (!$login_done) {
|
2012-11-16 17:21:35 +00:00
|
|
|
#Wait for command prompt
|
|
|
|
($prematch, $match) = $t->waitfor(Match => '/login[: ]*$/i',
|
|
|
|
Match => '/username[: ]*$/i',
|
|
|
|
Match => '/password[: ]*$/i',
|
|
|
|
Match => "/$prompt/",
|
|
|
|
Errmode => "return");
|
|
|
|
if ($verbose) {
|
|
|
|
print "3. prematch=$prematch\n match=$match\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($match =~ /$prompt/) {
|
|
|
|
$login_done=1;
|
|
|
|
} elsif ($match =~ /login[: ]*$/i or $match =~ /username[: ]*$/i or $match =~ /password[: ]*$/i) {
|
|
|
|
$output.="Login failed: bad login name or password\n";
|
|
|
|
return [1, $output];
|
2013-09-10 18:23:22 +00:00
|
|
|
} else {
|
|
|
|
if (!$nl_tried) {
|
|
|
|
# for some switches like BNT, user has to type an extra new line
|
|
|
|
# in order to get the prompt.
|
|
|
|
if ($verbose) {
|
|
|
|
print " add a newline\n";
|
|
|
|
}
|
|
|
|
$nl_tried=1;
|
|
|
|
if (! $t->put(String => "\n",
|
|
|
|
Errmode => "return")) {
|
|
|
|
$output.="Login disconnected\n";
|
|
|
|
return [1, $output];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($t->errmsg) {
|
|
|
|
$output.= $t->errmsg . "\n";
|
|
|
|
return [1, $output];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#check if the extra newline helps or not
|
|
|
|
if (!$login_done) {
|
|
|
|
#Wait for command prompt
|
|
|
|
($prematch, $match) = $t->waitfor(Match => "/$prompt/",
|
|
|
|
Errmode => "return");
|
|
|
|
if ($verbose) {
|
|
|
|
print "4. prematch=$prematch\n match=$match\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($match =~ /$prompt/) {
|
|
|
|
$login_done=1;
|
2012-11-16 17:21:35 +00:00
|
|
|
} else {
|
|
|
|
if ($t->errmsg) {
|
|
|
|
$output.= $t->errmsg . "\n";
|
|
|
|
return [1, $output];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 18:23:22 +00:00
|
|
|
|
2012-11-16 17:21:35 +00:00
|
|
|
}
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-29 21:52:15 +00:00
|
|
|
|
2012-11-13 22:03:57 +00:00
|
|
|
if (!$rc) {
|
2012-11-16 17:21:35 +00:00
|
|
|
$output.=$t->errmsg . "\n";
|
|
|
|
return [1, $output];
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$rc = 0;
|
2012-11-16 17:21:35 +00:00
|
|
|
my $try_more=0;
|
2012-11-13 22:03:57 +00:00
|
|
|
my @cmd_array=split(';', $args);
|
2012-11-16 17:21:35 +00:00
|
|
|
|
2012-11-13 22:03:57 +00:00
|
|
|
foreach my $cmd (@cmd_array) {
|
2012-11-16 17:21:35 +00:00
|
|
|
if ($verbose) {
|
|
|
|
print "command:$cmd\n";
|
|
|
|
}
|
2012-11-28 16:32:36 +00:00
|
|
|
|
2012-11-16 17:21:35 +00:00
|
|
|
while (1) {
|
|
|
|
if ($try_more) {
|
|
|
|
#This is for second and consequent pages.
|
|
|
|
#if the user disables the paging, then this code will never run.
|
|
|
|
#To disable paging (which is recommended),
|
|
|
|
#they need to add a command before any other commands
|
|
|
|
#For Cisco switch: terminal length 0
|
|
|
|
#For BNT switch: terminal-length 0
|
|
|
|
#For example:
|
|
|
|
# xdsh <swname> --type EthSwitch "terminal length 0;show vlan"
|
|
|
|
if (! $t->put(String => " ",
|
|
|
|
Errmode => "return")) {
|
|
|
|
$output.="Command $cmd failed: " . $t->errmsg() . "\n";
|
|
|
|
return [1, $output];
|
|
|
|
}
|
|
|
|
if ($verbose) {
|
|
|
|
my $lastline=$t->lastline();
|
|
|
|
print "---lastline=$lastline\n";
|
|
|
|
}
|
2012-11-29 21:52:15 +00:00
|
|
|
($prematch, $match) = $t->waitfor(Match => "/$more_prompt/i",
|
2012-11-16 17:21:35 +00:00
|
|
|
Match => "/$prompt/",
|
|
|
|
Errmode => "return",
|
2012-11-28 16:32:36 +00:00
|
|
|
Timeout=>10);
|
2012-11-16 17:21:35 +00:00
|
|
|
} else {
|
|
|
|
# for the first page which may contian all
|
|
|
|
if (! $t->put(String => "$cmd\n",
|
|
|
|
Errmode => "return")) {
|
|
|
|
$output.="Command $cmd failed." . $t->errmsg() . "\n";
|
|
|
|
return [1, $output];
|
|
|
|
}
|
|
|
|
if ($verbose) {
|
|
|
|
my $lastline=$t->lastline();
|
|
|
|
print "lastline=$lastline\n";
|
|
|
|
}
|
2012-11-29 21:52:15 +00:00
|
|
|
($prematch, $match) = $t->waitfor(Match => "/$more_prompt/i",
|
2012-11-16 17:21:35 +00:00
|
|
|
Match => "/$prompt/",
|
|
|
|
Match => '/password:\s*$/i',
|
|
|
|
Errmode => "return",
|
2012-11-28 16:32:36 +00:00
|
|
|
Timeout=>10);
|
2012-11-16 17:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($verbose) {
|
|
|
|
print "-----prematch=$prematch\nmatch=$match\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $error=$t->errmsg();
|
|
|
|
if ($error) {
|
2012-11-29 21:52:15 +00:00
|
|
|
$output.="Command $cmd failed: $error\n";
|
|
|
|
return [1, $output];
|
2012-11-16 17:21:35 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 21:52:15 +00:00
|
|
|
#
|
2012-11-16 17:21:35 +00:00
|
|
|
if ($try_more) {
|
2012-11-29 21:52:15 +00:00
|
|
|
#my @data=split("\n", $prematch);
|
|
|
|
#shift @data;
|
2012-11-16 17:21:35 +00:00
|
|
|
#shift @data;
|
|
|
|
#shift @data;
|
2012-11-29 21:52:15 +00:00
|
|
|
#$prematch=join("\n", @data);
|
2012-11-16 17:21:35 +00:00
|
|
|
#add a newline at the end if not there
|
|
|
|
my $lastchar=substr($prematch, -1, 1);
|
|
|
|
if ($lastchar ne "\n") {
|
|
|
|
$prematch .= "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output .= $prematch;
|
|
|
|
|
|
|
|
if ($match =~ /$more_prompt/i) {
|
|
|
|
$try_more=1;
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2012-11-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
$t->close();
|
|
|
|
return [0, $output];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-16 17:21:35 +00:00
|
|
|
|
|
|
|
|
2012-11-13 22:03:57 +00:00
|
|
|
1;
|