Here is some example code. Unless you have a Cisco controller, you can't actually run this and get meaningful output. The script completes, but the output to the various logs is all wrong. I think I need to find a way to wait for the prompt again so that it slows things down. I think the script is happening too quickly, the log inputs/outputs are getting created before the first subroutine has ended and the output for sub get_ap_summary shows up in the output log for show_inventory. The prompt is '(Cisco Controller) >', and I thought I set that in the properties of the session object, but perhaps the regex doesn't match? I thought if I changed the prompt in the session object that it would always wait (IOW, I thought the default setting is 'always wait for prompt').
#!c:/perl/bin/perl
use strict;
use warnings;
use Net::Telnet::Cisco;
use DBI;
use DBD::mysql;
use Env;
use File::Path;
use File::Basename;
use IO::Handle;
use Config::IniFiles;
use Term::ReadKey;
######################################################################
+##################################
# Global Variables ###################################################
+##################################
######################################################################
+##################################
my $username = "admin";
my $password = "admin";
my $host = "172.16.0.2";
my @output;
my $input_log;
my $output_log;
my $directory = "c:/perl/bin/test/";
#Check for the presence of the log file directory. If not there, crea
+te it
eval {
mkpath($directory);
if ($@) {
print "Couldn't create path: $@";
}
else {
mkpath($directory);
}
};
######################################################################
+##################################
# Global Variables ###################################################
+##################################
######################################################################
+##################################
######################################################################
+##################################
# Controller Connection ##############################################
+##################################
######################################################################
+##################################
system "cls";
print "Connecting to controller ...\n";
my $session = Net::Telnet::Cisco->new(
Host => $host,
Prompt => '/\(Cisco Controller\)/',
Errmode => 'return',
Timeout => 5);
$session->open($host);
# Wait for the username prompt and enter username
$session->waitfor('/^User:.*$/');
$session->print($username);
# Wait for the password prompt and enter password
$session->waitfor('/^Password:.*$/');
$session->print($password);
$session->cmd("config paging disable");
######################################################################
+##################################
# Controller Connection ##############################################
+##################################
######################################################################
+##################################
get_ap_summary();
show_run();
show_inventory();
cleanup();
exit();
######################################################################
+##################################
# Subroutines ########################################################
+##################################
######################################################################
+##################################
sub get_ap_summary {
print "\nGetting AP Summary ...\n";
#my @output;
$input_log = $directory . $host . "_get_ap_summary_in.log";
$output_log = $directory . $host . "_get_ap_summary_out.log";
$session->input_log($output_log);
$session->output_log($input_log);
@output = $session->cmd("show ap summary");
return;
}
sub show_run {
print "\nGetting Running Config ...\n";
#my @output;
$input_log = $directory . $host . "_get_run_config_in.log";
$output_log = $directory . $host . "_get_run_config_out.log";
$session->input_log($output_log);
$session->output_log($input_log);
@output = $session->cmd("show run-config");
return;
}
sub show_inventory {
print "\nGetting inventory ...\n";
#my @output;
$input_log = $directory . $host . "_get_inventory_in.log";
$output_log = $directory . $host . "_get_inventory_out.log";
$session->input_log($output_log);
$session->output_log($input_log);
@output = $session->cmd("show inventory");
return;
}
sub cleanup {
print "\nClosing telnet connection ...\n";
$session->cmd("config paging enable");
$session->close;
print "FINISHED\n";
}
######################################################################
+##################################
# Subroutines ########################################################
+##################################
######################################################################
+##################################