ssh to cisco router with expect CPAN

From: erik (ewitkop90_at_hotmail.com)
Date: 03/19/05


Date: 18 Mar 2005 19:53:27 -0800

I am ultimately trying to make CGI where we can Quality Assure Cisco
routers. My script does log into the router, but I don't think it is
storing in my array. I try to print the array and I get nothing. It
hangs within the check_logging_settings subscript.
(I have the ARGV scalars because I am testing it via command line
rather than the browser. )

But like I said the problem is definitely in teh check_logging_settings
sub script. Any advice would be appreciated.

#!/usr/bin/perl -w
#################################################################
# Global Variables #
#################################################################

use Expect;
use CGI(":standard");
use Term::ANSIColor;
#$device = param("device");
#$username = param("username");

#$password = param("password");

#$enable = "enable";

#$enable_password = param(enable_password");

$device = $ARGV[0];
$username = $ARGV[1] ;

$password = $ARGV[2];

$enable = $ARGV[3];

$enable_password = $ARGV[4];

$LoggingMsg = "Do you have logging back to the NTP server";

#Error Report Array
my @failed_tests;

#################################################################
# Create Session with router #
#################################################################

sub spawn_session
{
   #open session with router
   $command = Expect->spawn("telnet $device");

   #Wait for password prompt or send error.
   $command->expect($timeout, -re => "Username:") or do
   {
        print "\nFailed to get username prompt\n";
        exit 2;
   };

   #Send Tacacs Username to router, exit if Username is incorrect
   print $command "$username\r";

   $command->expect($timeout, -re => "Password:") or do
   {
        print "\nFailed to get password prompt\n";
        exit 4;
   };

   #Send password to router, exit if password is incorrect
   print $command "$password\r";

   $command->expect($timeout, -re => ">") or do
   {
        print "\nDid not get a \> in prompt\n";
        exit 5;
   };

   #Send enable pass to router, exit if password is incorrect
   print $command "$enable\r";

   $command->expect($timeout, -re => "assword") or do
   {
        print "\nDid not get a \> in prompt\n";
        exit 5;
   };

   #Send enable pass to router, exit if password is incorrect
   print $command "$enable_password\r";
} #end sub

#################################################################
# Ensures that only one interface has manage ssh set #
#################################################################
sub check_logging_settings

{
   my $no_logging = "You are missing logging 199.11.x.xx";

   $command->clear_accum();
   print $command "show config \| inc logging 199.11.x.xx\r";
   unless ($command->expect($timeout, -re, '->')) {
        return "Never got ssh prompt".$command->exp_error()."\n";
   }
   my $read = $command->exp_before();
   my @read = split ('\n', $read);

   my $x = 0;
   my $count = 0;

   for ($x=0; $x<$#read; $x++)
   {
        if ($read[$x] =~ /^logging 199.11.1.22$/)
           { $count += 1; }
   }
   if ($count < 1)
   {
     push (@failed_tests, $no_logging);
     $action = "FAIL";
   }
   else
   {
     $action = "OK";
   }#end if
print "$action We got here";
}

#################################################################
# Exits the Telnet Session #
#################################################################
sub exit_session
{
   print $command "exit\r";
}#end sub

#################################################################
# Prints the failed test error descriptions #
#################################################################
sub print_error_report
{
print <<html1
Conent-Type: text/html\n\n
<html><head><title>QA REPORT</title></head>
<body bgcolor="#ffffff">
<p>
<h2>QA Results</h2>
<ul>
<body>
<b>$LoggingMsg</b><br>
</body>
print <<EndFooter;
</ul>
<p>
</body>
</html>
html1
}
#################################################################
# Main Program Module #
#################################################################
spawn_session();
check_logging_settings();
exit_session();
print_error_report();



Relevant Pages

  • Re: Netzwerkverbindung - Problem
    ... Router ist auch zu erreichen - früher hatte der WiFi Router 2 Adressen ... (2 und 3 Sub), der Folgerouter hatte eine Adresse aus dem 3er Sub. ... waren (entsprechend eingestellt - da der äußere Router normal keinen Ping ... Jetzt ist der WiFi Router neu konfiguriert worden - er hat nur eine Adresse ...
    (microsoft.public.de.german.win2000.networking)
  • Re: Get Visio Shape Properties
    ... Add a custom property to your router shape and call it "IpAddress". ... Sub Telnet ... Which opens up a terminal session to the device. ...
    (microsoft.public.visio.developer)
  • CGI calling an expect script problem
    ... I try to make it into a CGI, ... sub spawn_session ... #Send Tacacs Username to router, exit if Username is incorrect ...
    (comp.lang.perl.misc)
  • Re: 2 Netzwerke miteinander verbinden
    ... Subnetz: 255.255.0.0 ... Sub: 255.255.255.0 ... Router W2K3-Server ... extern-IP: 192.168.0.7 ...
    (microsoft.public.de.german.windows.server.general)
  • Re: Obtaining my IP, strange results
    ... On my LinkSys DSL router I use a Perl script which uses LWP and HTML::TreeBuilder to get the status page from the router and grok out its IP address. ... sub parse_contents { ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)