0

I'm using Perl to automate a script on some Cisco routers. The basic implementation is that it connects to a given list of routers, one after the other and collects the needed information. Each time the script connects to a new router it requests a password, which is the same for all routers.

I have the following code to capture the password request and input the password:

    $session->expect(5, 
        [ 'password:' => sub {
            $session->send("$password\r");
                #exp_continue;
            }]);   

The problem is that sometimes the password isn't accepted the first time, for whatever reason. Is there a way to repeat the above code until the password is accepted?

DJDMorrison
  • 1,302
  • 2
  • 17
  • 34

1 Answers1

2

Super simple implementation:

my $tries = 0;
my $max = 5;
while ($tries < $max) {
    $tries++;
    # do your stuff
    # assuming that you can assess the success of the password sending:
    last if <test for successful condition>;
}

You can set the maximum number of attempts to whatever is appropriate for your situation.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
  • It was more of a question asking if expect returned something that would indicate that inputting the password failed but thank you anyway! I used the expect inside a while loop which continued trying until something was outputted that is only outputted when the device is signed in. – DJDMorrison Sep 12 '14 at 23:38