CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Script not exiting out

 



pedth01
New User

Jun 11, 2015, 7:38 AM

Post #1 of 3 (1348 views)
Script not exiting out Can't Post

Hi Guys,

I have a script that is running but doesn't exit out after it does what it's designed to do. Any ideas? I am new to perl.

Thanks

Tmo


pedth01
New User

Jun 11, 2015, 7:40 AM

Post #2 of 3 (1346 views)
Re: [pedth01] Script not exiting out [In reply to] Can't Post


Code
#!/opt/SPECTRUM/bin/perl -w 

# This script will capture the startup configuration of a
# MRV SSH device and print it to STDOUT.
#
# Error Codes:
# 0 = Success
# 255 = Usage error
# 254 = Invalid timeout value
# 253 = Unexpected output
# 252 = Connection error
# 251 = Login error
# 250 = Login Time Out
# 249 = Enable error
# 244 = Error retrieving configuration
#

use strict;
use warnings;
use Net::SSH::Expect;

$ENV{'PATH'} = "/usr/bin:". $ENV{'PATH'};

my %CFG = (
'pageoff' => 'no cli-paging', # disable paging command
'configcmd' => 'sh startup-config', # config command
'lp' => '>', # login prompt
'ep' => '#', # enabled prompt
'enablecmd' => 'enable' # enabled command
);

### Main ###
if( $#ARGV != 4 && $#ARGV != 5 )
{
print "Usage: $0 <device IP> <user> <pass> <enable_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds>\n";
print STDERR "Usage: $0 <deviceIP> <user> <pass> <enable_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds>\n";
exit 255;
}
elsif( $ARGV[4] < 1 || $ARGV[4] > 600 )
{
print "$ARGV[4] is the login timeout and must be an int between 1 and 600 seconds\n";
print STDERR "$ARGV[4] is the login timeout and must be an int between 1 and 600 seconds\n";
exit 254;
}
elsif( $#ARGV == 5 && ( $ARGV[5] < 1 || $ARGV[5] > 600 ) )
{
print "$ARGV[5] is the capture timeout and must be an int between 1 and 600 seconds\n";
print STDERR "$ARGV[5] is the capture timeout and must be an int between 1 and 600 seconds\n";
exit 254;
}
else
{
my $capture_timeout = $ARGV[4];
if( $ARGV[5] )
{
$capture_timeout = $ARGV[5];
}

my $errorCode = 1;
my @data;
my $errorString = "\nHost $ARGV[0]: \n";

($errorCode, @data) = GetConfig( $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3],
$ARGV[4], $capture_timeout );

if( $errorCode == 0 )
{
# Success. The running configuration
# content is in the data variable

foreach ( @data ) { print "$_\n" }; # print the configuration to STDOUT
exit 0;
}
else
{
print STDERR $errorString;

if( $errorCode == 253 )
{
print STDERR join " ", @data, "\nEnable password may be invalid\n";
}
else
{
print STDERR join " ", @data, "\n";
}

exit $errorCode;
}
}

exit 0;

sub GetConfig
{
my $deviceIP=shift;
my $user=shift;
my $pass=shift;
my $epass=shift;
my $login_timeout=shift;
my $capture_timeout=shift;
my @config;
my $msg;

my $ssh = Net::SSH::Expect->new ( host => $deviceIP,
user => $user,
password=> $pass,
raw_pty => 1,
no_terminal => 1,
timeout => $login_timeout
);

my $login_output;
eval { $login_output = $ssh->login(); };
if( $@ )
{
if ($@ =~ /timed out/i) {
$msg = "Login has failed\. Output: Login timed out";
return( 250, $msg );
} else {
$msg = "Login has failed\. Output: $login_output";
return( 251, $msg );
}

}

#first try login without username and just password
if( $login_output !~ /[$CFG{'lp'}]\s*\z/ )
{
$msg = "Login has failed. Didn't see device prompt as expected.\n";
$ssh->close();
return( 252, $msg );
}

#Some devices require enablement for password.
if( $login_output !~ /$CFG{'ep'}\s*\z/ )
{

my $enable = $ssh->exec( $CFG{'enablecmd'} );
# if we have a password prompt after enable, send the password
if( $enable =~ /[Pp]assword:/ )
{
my $enablepass = $ssh->exec( $epass );

# did the enable password fail?
if( $enablepass !~ /\#\s*\z/ )
{
$msg = "Enable password failed.";
$ssh->close( );
return( 249, $msg );
}
}
# we didn't require a password, but did we get the enable prompt?
elsif( $enable !~ /$CFG{'ep'}\s*\z/ )
{
$msg = "Enable mode prompt not found.";
$ssh->close();
return( 249, $msg );
}
}

# disable paging
# different commands for different devices, if they don't
# work then we will get messages about problems later
# specifically the "No prompt after get_config_command" error
# errmsg doesn't get set when these error and if we use print
# and getlines to read for errors it causes problems with print "sh run"
# later.
$ssh->exec( $CFG{'pageoff'} );

$ssh->send( $CFG{'configcmd'} );
$ssh->timeout( $capture_timeout );
$ssh->peek(0);

while( my $line = $ssh->read_line() )
{
# get configuration content

if( $line !~
/$CFG{'configcmd'}|Building configuration|Current configuration|^\s*$/ )
{
push @config, $line;
}
}

if( @config <= 0 )
{
$msg = "No data retrieved, the capture timeout may be too low.";
$ssh->close();
return( 244, $msg );
}

if( scalar grep { $_ =~ /^%/ } @config )
{
# Ensure show start actually returned the config and not an error message containing '%'
return( 253, @config );
}

return( 0, @config ); # everything was okay, return the captured data
}



FishMonger
Veteran / Moderator

Jun 11, 2015, 8:38 AM

Post #3 of 3 (1340 views)
Re: [pedth01] Script not exiting out [In reply to] Can't Post

If the script never finishes/exits then how do you know it completed everything it was designed to do?

You need to add some debugging print statements to narrow down where it's hanging.

You could also step through it via perl's debugger.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives