develooper Front page | perl.beginners | Postings from April 2010

Problems with "show tech" using the Net::Telnet Module

Thread Next
From:
Asterix
Date:
April 20, 2010 02:28
Subject:
Problems with "show tech" using the Net::Telnet Module
Message ID:
058c6e09-b659-408d-8eaa-3ca2bed251b2@u21g2000yqc.googlegroups.com
I've made a little script to capture the output of networking
devices.
Everything works fine, until I use the "show tech" command in an Cisco
device.
The script does not save anything form the "show tech",
but I can see that the TELNET Session gets back some output.
But I can't figure out how to save this output from "show tech" into a
file.
There is no problem with commands which produce smaller output.
I don't want to use the "Net::Telnet:Cisco" Module:

Here is the part, that does not work the way I want:

 #!/usr/bin/perl
# Autor: 		Ing. Gerhard Hermann Lange
# Date:  		12/04/2010
# Description:	Script for doing a TELNET to a CISCO devices and
executing some comands.
#				Long outputs from the TELNET Session (-> show tech) is not
possible
#				due to the TELNET session timeout of 5 seconds
use Net::Telnet;
#
my $prg_name = $0; $prg_name =~ s|^.*[\\/]||;
# Usage:
my $usage="Syntax: $prg_name <hostname> <ip> <username> <password>
<enable-pwd>\n";
if (! defined ($ARGV[0])) { print "$usage"; exit; }
#
$SIG{'INT'} = sub {
	# Subprocedure for finishing all the work
	# printf OUTPUT_FILE "@output\n\n";
	close(OUTPUT_FILE);
	print STDERR "Termination of program ... !\n";
	exit;
};
#
$hostname = $ARGV[0];
$ip = $ARGV[1];
$username = $ARGV[2];
$password = $ARGV[3];
$enpwd = $ARGV[4];
$cmd_file = "cmds.txt";
#
my ($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time);
$mon++; $year=$year+1900;
my $now=sprintf("%.4d%.2d%.2d%.2d%.2d", $year, $mon, $mday, $min,
$sec);
my $today=sprintf("%.2d/%.2d/%.4d", $mday, $mon, $year );
#
# $out_file=$ip;
$out_file=$ip."_$now".".cfg";
#
# For debugging the TELNET Session: uncomment this line !
# $filename="telnet_dump.txt";
#
$errmode='return'; # Default: 'die'
#
# Set cmd_remove_mode to the number of lines to remove (0 in this
case).
# http://www.perlmonks.org/?node_id=736670
$errmode="return";
$telnet = new Net::Telnet ( Timeout=>5, Errmode=>$errmode ,
cmd_remove_mode => '0', Dump_Log =>
$filename);

# $telnet = new Net::Telnet ( Timeout=>3, Errmode=>$errmode ,
Output_record_separator => "\r",cmd_remove_mode => '2', Dump_Log =>
$filename);
# -output_record_separator => "\r");
$telnet->open($ip);
#
$telnet->waitfor('/Username: $/i');
$telnet->print($username);
$telnet->waitfor('/Password: $/i');
$telnet->print($password);
#
# Wait for the prompt, send "enable" and enable-pwd
@output = $telnet->waitfor('/>/');
$telnet->print('enable');
$telnet->waitfor('/Password:/i');
$telnet->print($enpwd);
#
@output = $telnet->waitfor('/#/i');
#
open(OUTPUT_FILE, ">>$out_file");
printf OUTPUT_FILE "IP Address: $ip\n";
printf OUTPUT_FILE "Hostname:   $hostname\n";
printf OUTPUT_FILE "Data:       $today\n";
printf OUTPUT_FILE "\n";
#
# Creat an universal Prompt for this hostname
my $prompt = $hostname."#";
$telnet->prompt("/$prompt\$/");
@output = $telnet->cmd(String => "terminal length 0");
printf OUTPUT_FILE "@output\n\n";
#
open(CMDFILE, "<$cmd_file");
while (my $record = <CMDFILE>) {
	if ($record !~ /^(#|!|\s)/) {
		chomp($record);
		my @record_array = (split(/;/,$record));
		my $command = $record_array[0]; chomp($cmd);
		#
		$telnet->prompt("/$prompt\$/");
		@output = $telnet->cmd(String => "$command", Timeout=>30);
		#
		printf OUTPUT_FILE "@output\n\n";
	}
}
# Close the command File for this device
close(CMDFILE);
#
$telnet->print('exit');
close(OUTPUT_FILE);


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About