qr '[#>][\s]?$',
但是在登录到交换机时,提示如下:
sw-sn2410 [standalone:master]>
package Session;
use strict;
use warnings;
use Expect;
use IO::Pty;
use Data::Dumper;
use Time::HiRes qw(usleep);
sub new
{
my $class = shift;
my $self = {
_hostnum => shift,
_hostname => shift,
_username => shift,
_password => shift,
_mode => shift,
_timeout => 300,
_spawn_ok => 0,
_output => "",
_rc => 255,
_exp => undef,
};
$self->{_exp} = _create_process($self);
bless $self, $class;
return $self;
}
##############################################################################
# Function Name : _create_process
#
#
# Notes :
# This function spawn to process based on connection mode & login to machine
##############################################################################
sub _create_process
{
my ($selfObj) = @_;
my $cmdStr;
my $exp = Expect->new;
my $timeout = $selfObj->{_timeout};
$selfObj->{_spawn_ok} = 0;
$selfObj->{_mode} = lc($selfObj->{_mode});
if ($selfObj->{_mode} eq "telnet" )
{
$cmdStr = "$selfObj->{_mode} $selfObj->{_hostname}";
}
elsif ($selfObj->{_mode} eq "ssh")
{
`rm -rf /.ssh`;
$cmdStr = "$selfObj->{_mode} -o StrictHostKeyChecking=no $selfObj->{_username}\@$selfObj->{_hostname}";
}
elsif ($selfObj->{_mode} eq "rsh")
{
$cmdStr = "$selfObj->{_mode} $selfObj->{_hostname} -l $selfObj->{_username}";
}
else
{
printf("FileName : Session.pm , Can't initiate the connection !! Support not available for $selfObj->{_mode}");
return -1;
}
$exp->raw_pty(1);
$exp = Expect->spawn($cmdStr) or die "$selfObj->{_mode} to $selfObj->{_hostname} failure!! , Please check the connection & retry again: $!\n";
if (!defined($exp))
{
print "$selfObj->{_mode} to $selfObj->{_hostname} failure!! , Please check the connection & retry again\n";
return -1;
}
`sleep 2 `;
$exp->log_user(0);
$exp->log_file("$ENV{TSWORK}/session.log");
#$SIG{WINCH} = \&_winch($exp);
if ((defined ($ENV{Debug}) && $ENV{Debug} > 1 ))
{
print ("\nFunction : _create_process\n\n");
$exp->debug(3);
$exp->exp_internal(0);
$exp->log_user(1);
}
$selfObj->{_exp} = $exp;
print ("Creating connection to $selfObj->{_hostname}...\n");
my $rc = do_login($selfObj,$selfObj->{_hostname},$selfObj->{_username},$selfObj->{_password});
($rc != 0) ? return $rc : 0;
$selfObj->{_spawn_ok} = 1;
#prompt setup
set_cmd_prompt($selfObj);
#$exp->slave->clone_winsize_from(\*STDIN);
my $winsize = pack('SSSS', 100, 5000, 0, 0); # rows, cols, #pixelsX,
ioctl($exp->slave(), &IO::Tty::Constant::TIOCSWINSZ, $winsize);
return $exp;
}
##############################################################################
# Function Name : _winch
#
#
# Notes :
# This handler used to handle screen size change
##############################################################################
sub _winch
{
my ($exp) = @_;
$exp->slave->clone_winsize_from(\*STDIN);
kill WINCH => $exp->pid if $exp->pid;
$SIG{WINCH} = \&_winch;
}
##############################################################################
# Function Name : do_login
# IN => lpar - Name of the lpar/machine
# IN => username - username of the machine
# IN => password - password of the machine
#
# Return Value :
# Success : 0, any other value for failure
#
# Notes :
# This fucntion used to login into machine & set the prompt
##############################################################################
sub do_login
{
my ($selfObj,$lpar,$username,$password) = @_;
my $exp = $selfObj->{_exp};
my $timeout = $selfObj->{_timeout};
my $rc = -1;
$exp->send("\r");
$exp->expect($timeout,
[
qr'[lL]ogin:[\s]?[a]?[s]?[\s]?$',
sub
{
my $fh = shift;
$fh->send($username."\r");
`sleep 3 `;
exp_continue;
}
],
[
qr'[uU]sername:[\s]?$',
sub
{
my $fh = shift;
$fh->send($username."\rn");
`sleep 3 `;
exp_continue;
}
],
[
qr'[pP]assword:[\s]?$',
sub
{
my $fh = shift;
$fh->send($password."\r");
`sleep 3 `;
$exp->send("\r");
$exp->send("\r");
$rc = 0;
exp_continue;
}
],
[
eof =>
sub
{
print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
}
],
[
timeout =>
sub
{
print("FileName : Session.pm , Timeout : Command couldn't complete.\n");
}
],
'-re', qr'[#$>:]\s?$', # wait for shell prompt, then exit expect
);
$exp->send("\r");
usleep(1000);
$exp->expect(3,
[
qr '[#>][\s]?$',
sub {
exp_continue;
}
]
);
return $rc;
}
##############################################################################
# Function Name : execute_rm_cli_cmd
# IN => cmdStr - Command to execute
# IN => reqLog - 1 - Log is needed , 0 - Log is Not needed
# IN => reqRC - 1 - return code is needed , 0 - return code is Not needed
#
# Return Value :
# cmd_output - command output
#
# Notes :
# This fucntion execute the command & set command ouput & retcode into the
# Object.
##############################################################################
sub execute_rm_cli_cmd
{
my ($selfObj,$cmdStr,$reqLog)= @_;
my $output;
my $str = "$selfObj->{_hostname}\@$selfObj->{_username} # ";
set_exp_setting($selfObj);
if ($selfObj->{_spawn_ok} == 0)
{
print("FileName : Session.pm , Connection already closed , So can't run the command. \nplease open the connection & run \n");
return "";
}
print("Command : $cmdStr\n");
$selfObj->{_exp}->clear_accum();
$selfObj->{_exp}->send("\r");
$selfObj->{_exp}->expect($selfObj->{_timeout},
[
qr '[#>]\s?$',
sub
{
my $fh = shift;
$fh->send($cmdStr ."\r");
if ( defined($reqLog) && $reqLog == 1 )
{
$fh->expect($selfObj->{_timeout}, '-re', $str);
$output = $fh->exp_before();
$output =~ s/\Q$cmdStr\E//;
$output =~ s/^\s+//;
$output =~ s/\s+$//;
$selfObj->{_output} = $output;
chomp($selfObj->{_output});
if (defined($selfObj->{_output}))
{
print("Cmd_output :\n$selfObj->{_output}\n");
}
$fh->send_slow(1,"\r");
$fh->expect($selfObj->{_timeout}, '-re', $str );
$fh->clear_accum();
$fh->send("echo \$?\r");
$fh->expect($selfObj->{_timeout}, '-re', $str );
$output = $fh->exp_before();
$output =~ s/\Qecho \E\$\?//;
$output =~ s/^\s+//;
$output =~ s/\s+$//;
$selfObj->{_rc} = $output;
chomp($selfObj->{_rc});
if (!defined($selfObj->{_output}))
{
$selfObj->{_rc} = 255;
}
print("ret_code : $selfObj->{_rc}\n");
}
print ("\n");
}
],
[
eof =>
sub
{
print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
$selfObj->close_con();
}
],
[
timeout =>
sub
{
print("FileName : Session.pm , Timeout : Command couldn't complete.\n");
$selfObj->close_con();
}
],
);
if (defined($selfObj->{_output}))
{
return $selfObj->{_output};
}
}
##############################################################################
# Function Name : get_status
#
#
# Notes :
# Retur the status of connection
##############################################################################
sub get_status
{
my ($selfObj) = @_;
my $exp = $selfObj->{_exp};
my $stat = $exp->exp_exitstatus();
$stat = (defined($stat))? $stat : 0;
return $stat;
}
##############################################################################
# Function Name : close_con
#
#
# Notes :
# Close the connection.
##############################################################################
sub close_con
{
my ($selfObj)= @_;
my $exp = $selfObj->{_exp};
$selfObj->{_exp}->send("exit\r\r");
$selfObj->{_spawn_ok} = 0;
$selfObj->{_exp}->DESTROY();
}
##############################################################################
# Function Name : set_timeout
#
#
# Notes :
# Set the command timeout
##############################################################################
sub set_timeout
{
my ($selfObj,$secs)= @_;
$selfObj->{_timeout} = $secs;
}
##############################################################################
# Function Name : execute_switch_cmd
# IN => cmdStr - Command to execute
# IN => reqLog - 1 - Log is needed , 0 - Log is Not needed
#
# Return Value :
# cmd_output - command output
#
# Notes :
# This fucntion execute the command & set output into cmd_ouput
##############################################################################
sub execute_switch_cmd
{
my ($selfObj,$cmdStr)= @_;
my $output;
set_exp_setting($selfObj);
if ($selfObj->{_spawn_ok} == 0)
{
print("FileName : Session.pm , Connection already closed , So can't run the command. \nplease open the connection & run \n");
return "";
}
print("Command : $cmdStr\n");
if (!defined($ENV{SWITCH_TEST}))
{
$ENV{SWITCH_TEST} = "yes";
$selfObj->{_exp}->clear_accum();
$selfObj->{_exp}->send("\r");
usleep(1000);
$selfObj->{_exp}->expect(3,
[
qr '[#>]\s?$',
sub {
exp_continue;
}
]
);
}
$selfObj->{_exp}->send("\r");
usleep(1000);
$selfObj->{_exp}->expect($selfObj->{_timeout},
[
qr '[#>]\s?$',
sub {
my $fh = shift;
$fh->send($cmdStr . "\r");
$fh->expect($selfObj->{_timeout}, '-re', '[#>]\s?$' );
$output = $fh->exp_before();
$output =~ s/\Q$cmdStr\E//;
$output =~ s/^\s+//;
$output =~ s/\s+$//;
$selfObj->{_output} = $output;
open(FILEHANDLE, ">$ENV{TSWORK}/temp");
print(FILEHANDLE $output);
close(FILEHANDLE);
$selfObj->{_output} = `sed '\$d' $ENV{TSWORK}/temp`;
$selfObj->{_output} =~ s/^\s+//;
$selfObj->{_output} =~ s/\s+$//;
chomp($selfObj->{_output});
if (defined($selfObj->{_output}))
{
print("Cmd_output :\n$selfObj->{_output}\n");
}
print ("\n");
}
],
[
eof =>
sub
{
print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
$selfObj->close_con();
}
],
[
timeout =>
sub
{
print("FileName : Session.pm , Timeout : Command couldn't complete.\n");
$selfObj->close_con();
}
],
);
if (defined($selfObj->{_output}))
{
return $selfObj->{_output};
}
}
##############################################################################
# Function Name : set_cmd_prompt
#
# Return Value :
#
# Notes :
# This fucntion set the command prompt for the new session
##############################################################################
sub set_cmd_prompt
{
my ($selfObj)= @_;
if ($selfObj->{_hostnum} !~ /host/i)
{
return 0;
}
my $output;
my $str = '[#>]\s?$';
my $cmdStr = "export PS1=\"$selfObj->{_hostname}\@$selfObj->{_username} # \"";
$selfObj->{_exp}->clear_accum();
$selfObj->{_exp}->send("\r");
$selfObj->{_exp}->expect($selfObj->{_timeout},
[
qr '[#$>:][\s]?$',
sub
{
my $fh = shift;
$fh->send($cmdStr. "\r");
$fh->expect($selfObj->{_timeout}, '-re', '[#>][\s]?$' );
}
],
[
eof =>
sub
{
print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
$selfObj->close_con();
}
],
[
timeout =>
sub
{
print("FileName : Session.pm , Timeout : Command couldn't complete.\n");
$selfObj->close_con();
}
],
);
$selfObj->{_exp}->send("\r");
usleep(1000);
$selfObj->{_exp}->expect(3,
[
qr '[#>][\s]?$',
sub {
exp_continue;
}
]
);
}
sub set_exp_setting
{
my ($selfObj) = @_;
$selfObj->{_output} = "";
$selfObj->{_rc} = 255;
if ((defined ($ENV{Debug}) && $ENV{Debug} > 1 ))
{
$selfObj->{_exp}->debug(3);
$selfObj->{_exp}->exp_internal(0);
$selfObj->{_exp}->log_user(1);
print Dumper $selfObj;
}
else
{
$selfObj->{_exp}->debug(0);
$selfObj->{_exp}->exp_internal(0);
$selfObj->{_exp}->log_user(0);
}
$selfObj->{_exp}->notransfer(0) ;
$selfObj->{_exp}->clear_accum();
}
##############################################################################
# Function Name : execute_rm_icli_cmd
# IN => cmdStr - Command to execute
#
# Return Value :
# cmd_output - command output
#
# Notes :
# This fucntion execute the command & set cmd_ouput
##############################################################################
sub execute_rm_icli_cmd
{
my ($selfObj,$cmdStr)= @_;
my $output;
my $str = '[#>] ?$';
set_exp_setting($selfObj);
if ($selfObj->{_spawn_ok} == 0)
{
print("FileName : Session.pm , Connection already closed , So can't run the command. \nplease open the connection & run \n");
return "";
}
print("Command : $cmdStr\n");
$selfObj->{_exp}->clear_accum();
$selfObj->{_exp}->send("\r");
$selfObj->{_exp}->expect($selfObj->{_timeout},
[
qr '[#>]\s?$',
sub
{
my $fh = shift;
$fh->send($cmdStr. "\r");
$fh->expect($selfObj->{_timeout}, '-re', $str );
$output = $fh->exp_before();
$output =~ s/\Q$cmdStr\E//;
$output =~ s/^\s+//;
$output =~ s/\s+$//;
$selfObj->{_output} = $output;
chomp($selfObj->{_output});
if (defined($selfObj->{_output}))
{
print("Cmd_output :\n$selfObj->{_output}\n");
}
print ("\n");
}
],
[
eof =>
sub
{
print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
$selfObj->close_con();
}
],
[
timeout =>
sub
{
print("FileName : Session.pm , Timeout : Command couldn't complete.\n");
$selfObj->close_con();
}
],
);
if (defined($selfObj->{_output}))
{
return $selfObj->{_output};
}
}
sub execute_rm_cli_cmd_noscreen_output
{
my ($selfObj,$cmdStr,$reqLog)= @_;
my $output;
my $str = "$selfObj->{_hostname}\@$selfObj->{_username} # ";
set_exp_setting($selfObj);
if ($selfObj->{_spawn_ok} == 0)
{
print("FileName : Session.pm , Connection already closed , So can't run the command. \nplease open the connection & run \n");
return "";
}
$selfObj->{_exp}->clear_accum();
$selfObj->{_exp}->send("\r");
$selfObj->{_exp}->expect($selfObj->{_timeout},
[
qr '[#>][\s]?$',
sub
{
my $fh = shift;
$fh->send($cmdStr ."\r");
if ( defined($reqLog) && $reqLog == 1 )
{
$fh->expect($selfObj->{_timeout}, '-re', $str);
$output = $fh->exp_before();
$output =~ s/\Q$cmdStr\E//;
$output =~ s/^\s+//;
$output =~ s/\s+$//;
$selfObj->{_output} = $output;
chomp($selfObj->{_output});
$fh->send_slow(1,"\r");
$fh->expect($selfObj->{_timeout}, '-re', $str);
$fh->clear_accum();
$fh->send("echo \$?\r");
$fh->expect($selfObj->{_timeout}, '-re', $str);
$output = $fh->exp_before();
$output =~ s/\Qecho \E\$\?//;
$output =~ s/^\s+//;
$output =~ s/\s+$//;
$selfObj->{_rc} = $output;
chomp($selfObj->{_rc});
if (!defined($selfObj->{_output}))
{
$selfObj->{_rc} = 255;
}
}
}
],
[
eof =>
sub
{
print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
$selfObj->close_con();
}
],
[
timeout =>
sub
{
print("FileName : Session.pm , Timeout : Command couldn't complete.\n");
$selfObj->close_con();
}
],
);
if (defined($selfObj->{_output}))
{
return $selfObj->{_output};
}
}
##############################################################################
# Function Name : set_oem_prompt
#
# Return Value :
#
# Notes :
# This fucntion set the oem_setup_env command prompt for VIOS testing
##############################################################################
sub set_oem_prompt
{
my ($selfObj)= @_;
my $output;
my $str = '[#>][\s]?$';
my $cmdStr = "export PS1=\"$selfObj->{_hostname}\@$selfObj->{_username} # \"";
$selfObj->{_exp}->clear_accum();
$selfObj->{_exp}->send("\r");
$selfObj->{_exp}->expect($selfObj->{_timeout},
[
qr '[#$>][\s]?$',
sub
{
my $fh = shift;
$fh->send("oem_setup_env\r");
$fh->expect($selfObj->{_timeout}, '-re', '[#>][\s]?$' );
$fh->send($cmdStr. "\r");
$fh->expect($selfObj->{_timeout}, '-re', '[#>][\s]?$' );
}
],
[
eof =>
sub
{
print("FileName : Session.pm , ERROR: premature EOF in command execution.\n");
$selfObj->close_con();
}
],
[
timeout =>
sub
{
print("FileName : Session.pm , Timeout : Command couldn't complete.\n");
$selfObj->close_con();
}
],
);
$selfObj->{_exp}->clear_accum();
}
1;
尽管我能够登录到该交换机,但我的脚本无法执行命令。我想知道如何编写正则表达式,以便避免在切换提示中出现“ []”和“:”?