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

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

Thread Next
April 20, 2010 02:28
Problems with "show tech" using the Net::Telnet Module
Message ID:
I've made a little script to capture the output of networking
Everything works fine, until I use the "show tech" command in an Cisco
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
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:

# 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
#				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>
if (! defined ($ARGV[0])) { print "$usage"; exit; }
$SIG{'INT'} = sub {
	# Subprocedure for finishing all the work
	# printf OUTPUT_FILE "@output\n\n";
	print STDERR "Termination of program ... !\n";
$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,
my $today=sprintf("%.2d/%.2d/%.4d", $mday, $mon, $year );
# $out_file=$ip;
# 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
$telnet = new Net::Telnet ( Timeout=>5, Errmode=>$errmode ,
cmd_remove_mode => '0', Dump_Log =>

# $telnet = new Net::Telnet ( Timeout=>3, Errmode=>$errmode ,
Output_record_separator => "\r",cmd_remove_mode => '2', Dump_Log =>
# -output_record_separator => "\r");
$telnet->waitfor('/Username: $/i');
$telnet->waitfor('/Password: $/i');
# Wait for the prompt, send "enable" and enable-pwd
@output = $telnet->waitfor('/>/');
@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."#";
@output = $telnet->cmd(String => "terminal length 0");
printf OUTPUT_FILE "@output\n\n";
open(CMDFILE, "<$cmd_file");
while (my $record = <CMDFILE>) {
	if ($record !~ /^(#|!|\s)/) {
		my @record_array = (split(/;/,$record));
		my $command = $record_array[0]; chomp($cmd);
		@output = $telnet->cmd(String => "$command", Timeout=>30);
		printf OUTPUT_FILE "@output\n\n";
# Close the command File for this device

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About