Perl Scripting - Cisco Enable

MysticRyuujin

Limp Gawd
Joined
Oct 1, 2013
Messages
507
Hello,

I have some code that I use to execute Perl scripts on some Cisco devices. The problem is that I'm not a programmer, I'm more of a cut and paste and tweak various code kind of guy haha.

My script works perfectly fine if there is no Enable password required but I need a way to check for the enable password and send my password if it is required.

This is my current code:

Code:
use Expect;
use IO::Socket;
use Net::Ping;

my $exp = new Expect;

( $NAME, $PWD, $DNAME, $PORT, $LOG, $TICKET ) = @ARGV;

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

my $command = "ssh $NAME" . "@" . "$DNAME";

$exp->spawn($command) or die "Cannot spawn $command: $!\n";

my $connect = $exp->expect(
	10,
	[ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n"); exp_continue; } ],
	[ qr/assword:\s*$/ => sub { $exp->send("$PWD\n") if defined $PWD; } ],
);

$exp->expect(10,"$DNAME#");
$exp->send("term len 0\n");
$exp->expect(5,"$DNAME#");

So right now it checks for the typical SSH connection string asking if I'm sure I want to connect to whatever device and then it looks for the Password: prompt and then sends the password. That all works fine and dandy but I need a third check for:
$DNAME>

$DNAME being the hostname of the switch followed by the greater than symbol.

Generally I'll get $DNAME# and continue on my way but there are a few devices that require an enable password. The enable password is simply $PWD again so I just need it to send the following commands:
enable\n
$PWD\n

Part of my problem is that I don't know how to use expect when I don't want it to send unless something is true...I could send enable\n and then $PWD\n every time but I only want to send it if I get the greater than prompt $DNAME>

Thanks!
 
Anyone who cares here was my solution:
Code:
my $connect = $exp->expect
        (
                30,
                [ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n"); exp_continue; } ],
                [ qr/assword:\s*$/ => sub { $exp->send("$PWD\n"); } ],
        );
        $result = $exp->expect(30, "#", ">");
        if ($result == 2)
        {
                $exp->send("enable\n");
                $result = $exp->expect(30, "assword:", "#");
                if ($result == 1)
                {
                        $exp->send("$ENABLE\n");
                        $result = $exp->expect(30, "assword:", "#");
                        if ($result == 1)
                        {
                                print LOG "Enable Password Rejected\n";
                                exit();
                        }
                }
        }
$exp->send("term len 0\n");
$exp->expect(10,"#");
 
Back
Top