Re: autoexpect

From: Marko Milivojevic <markom_at_markom.info>
Date: Wed, 30 Sep 2009 17:07:42 +0000

On Wed, Sep 30, 2009 at 16:57, <dls152_at_cox.net> wrote:
> I am trying to create script to telnet to all my routers and switches so I can add some commands. I had heard about autoexpect but do not use linux and don't know expect. Do anyone know of any options?

Use at your own risk.

------------------------------8<------------------------------
#!/usr/bin/perl -w

#
# Created by Marko Milivojevic <markom_at_markom.info>
#
# No warranty of any sort. Use at your own risk.
#

use Net::Telnet::Cisco();
use strict;

my ($f, $host, $user, $pass, $enab, $cmdf, $lstf, @cmds);

$cmdf = "commands.txt";
$lstf = "list.txt";

$user = "USERNAME";
$pass = "PASSWORD";
$enab = "ENABLE_PASS";

sub ConfigureRouter {
        my ($t, $host, $cmd);

        $host = $_[0];

        if (!($t = Net::Telnet::Cisco->new(Host => "$host", Errmode =>
"return"))) {
                printf("Could not open session to '%s'\n", $host);
                return;
        };
        if (!($t->login($user, $pass))) {
                printf("Could not log into '%s' as '%s'\n", $host, $user);
                return;
        }
        $t->enable($enab);
        print("Login successful. Setting up ");
        $t->print("terminal length 0");
        print $t->waitfor("/#/");
        $t->print("conf t");
        foreach $cmd (@cmds) {
                print $t->waitfor("/#/");
                $t->printf("%s", $cmd);
        }
        print $t->waitfor("/#/");
        $t->print("end");
        print $t->waitfor("/#/");
        $t->print("write memory");
        print $t->waitfor("/#/");
        $t->close();
}

#
# Main Body
#
open (CF, "< " . $cmdf) or die "Can't read the command file '$cmdf'";
@cmds = <CF>;
close(CF);

open (IF, "< " . $lstf) or die "Can't read the router list file '$lstf'";

while (defined ($host = <IF>)) {
        chomp($host);
        if ($host ne "") {
                printf("\n\n\n%s %s %s\n", "-" x 22, $host, "-" x 22);
                ConfigureRouter($host);
        }
}
close(IF);
------------------------------8<------------------------------

--
Marko
CCIE #18427 (SP)
My network blog: http://cisco.markom.info/
Blogs and organic groups at http://www.ccie.net
Received on Wed Sep 30 2009 - 17:07:42 ART

This archive was generated by hypermail 2.2.0 : Sun Oct 04 2009 - 07:42:04 ART