Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wait for output to finish 1

Status
Not open for further replies.

Odaroff

Technical User
Nov 5, 2008
5
US
I have the below script that will login to a Cisco router, then do a show ver and show run command outputing to a file.

My problem is often times the show run output can take a couple of minutes to complete, I could put in a sleep however there are over 1200 routers I run this against so I'd rather not drag it out for a long time.

Is there a way to watch for the output to finish and return to the command prompt or wait for "End" as Cisco configs always end with that.

Thanks for any help and yes I inherited this script :p

========================================================

#!/usr/bin/perl

# Include statements...

use Net::Telnet::Cisco;



# Global variables...

$mylog = "logfile";

$login = "user";

$pass = "passwordhere";

$enable = "enablepasshere";

# Pass the router name to remote variable...

if ($ARGV[0])

{

$remote=$ARGV[0];

}







# Run the sequence...

&connect();

crisco("terminal length 0");

crisco("show ver");

crisco("show run");

sleep(5);

&disconnect();









# Subroutines

# Writes to the logfile...

sub mylog

{

my $msg = shift;

$date=`date`;

chop($date);

open(LOG, ">>$mylog");

print LOG "$date : $msg\n";

close(LOG);

}

# Disconnects from the router...

sub disconnect

{

mylog("\nDisconnecting...\n");

$session->close();

}



# Exits to prompt & writes memory...

sub wr_mem

{

crisco("");

crisco("write memory");

}



# Simplifies sending commands to router via Net::Telnet::Cisco $session commands...

sub crisco

{

my $ioscmd = shift;

mylog("\ncrisco(): $ioscmd\n");

dir($session->errmsg) unless($session->cmd("$ioscmd"));

}



# Connects to the router & enters enable mode...

sub connect

{

print "Connecting to $remote...\n\n";

mylog("Connecting to $remote");

$session = Net::Telnet::Cisco->new(Host=>"$remote",Input_log=>"$remote.log",Errmode=>sub { my $errmsg = shift; },) or mylog("Connection to $remote FAILED!!");

$session->login("$login","$pass") or mylog("Login FAILED");

mylog("Enable Session FAILED") unless($session->enable("$enable"));

dir($session->errmsg) unless($session->enable("$enable"));

}


 
I'm not sure what your asking. Net::Telnet will wait till timeout for the output. So yo don't have to do anything to make it wait. To increase your speed look into forking or threads.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
This script will often finish before the show run command finishs - the sho ver will complete but often times I will just get the

building configuration...

or a parital listing before the script moves on to the next router.

It is being fed by a wrapper executed by "./loop" so that might be part of it see below:

-----------------------------------------------

#!/usr/bin/perl

use Net::Telnet::Cisco;


# Global variables

$listfile = "routerlist";

# Loop process...
open(ROUTER_LIST,$listfile);
while ($remote = <ROUTER_LIST>)
{
push(@listfile,$remote);
chop($remote);
system("./showrun $remote");
}
exit(0);

-----------------------------------

routerlist has a list off all the router names
 
increase your timeout to something high like 300

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks but as I said that is what I am trying to avoid based on the number of devices...
 
I've usually done something like that using expect - activestate makes a version for windows if that's what you're looking for. Also there are perl modules on cpan that provide the same functionality.
 
Actually I'm running this on Linux and any help you can provide with the coding or if you can point me in the direction of some examples would be greatly appreciated.
 
Did you look up forking or threads? Check out parallel::forkmanager in cpan.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
OK, now that I have looked up the parallel::forkmanager I can see where it can help me :) once I get this working right...

Problem is I still don't know how to look for the EOF - in this case the word "End" before I run the disconnect module.

&connect();

crisco("terminal length 0");

crisco("show ver");

crisco("show run");

**********************************************************
(I need to wait for the word "End" from the "Show Run" command before I move on)
**********************************************************
&disconnect();
 
If your using the Net::Telnet module you can do things like
$telnet->print "show run";
$telnet->waitfor '/End/';

(probably not anywhere near the correct syntax.. check the module)

but you can do print and waitfor. It's not always fun to intermix these with the cmd's but it will work.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top