Link to home
Start Free TrialLog in
Avatar of Fubyou
Fubyou

asked on

automate telnet login and excute cisco command procedures (batch)

    I have asked this question before and noone replied. So here it goes again. I have like 25-30 switchs on my network; 2950s to be exact. I need to log into each switch and copy each config over to a central repository for disaster recovery or a way to log into each switch and run some commands like mac-address-table for all ports on a particular switch. The point is i need to automate the login and excute cisco command procedures (batch the procedure). Telnet through DOS doesn't enable you to feed a password to it as far is i know. There is a program called "expect" that i guess has that capability but im short on tutorials and resources to configure the program. Has anyone done this and how?
Avatar of PennGwyn
PennGwyn

We use Kermit as a scriptable console for this.

Avatar of Don Johnston
You can use the advanced version of Hyperterm. It's called HyperAccess and it allows you to build scripts to automate tasks like that.

http://www.hilgraeve.com/hyperaccess/win32/index.html

-Don
You know, I use Perl to do this sort of thing. You can install Perl on Windows. there is a module called Net::Telnet::Cisco that is written to understand Cisco prompts. I once wrote a script to tftp a bunch of configs to my machine so I could work with them, without having to take the time to do it by hand.

Of course, now I use Ciscoworks for that. But Perl is the perfect tool for your needs. Here's a script I wrote to do that. There were some quirks here, like we didn't need the enable password- they had a local user with privilege 15 (a really stupid idea). But the module above supports the enable password so that's not a problem, I've used it for other things since.

# USE -F OPTION TO READ LIST OF ROUTERS *OR*
# USE THESE INDIVIDUAL OPTIONS TO PULL AN INDIVIDUAL CONFIG:
# -h [hostname or IP]
# -p [password -optional- script will create standard station password]
# -u [username-probably "root"]
#
$username="[put a username here]"; # login name if needed
$infile="routerlist.txt"; # list of routers to read from
#
use Net::Telnet::Cisco;
use Getopt::Long;
GetOptions(
      "host=s"     =>      \$host,
      "password=s" =>      \$pass,
      "username=s" =>      \$username,
      "file"       => \$usefile,
);
if ($usefile) {
      open (LIST,"$infile") or die "Couldn't open file $infile";
      @list=<LIST>;
      foreach (@list) {
                  ($host)=split(/\s+/,$_);
                  &TELNET;
                  }
} else {
      &TELNET;
}
# End of script
      
sub TELNET {
      ($station) = $host =~/(^\w+)/;
      ($initial) = $host =~/(^\w)/;
      $pass = join '',$initial,"123",$station;
      $t = Net::Telnet::Cisco->new(
            Timeout => 10,
            Host    => $host
                  );
      $t->errmode("return");
      print "Trying $host\n";
      $t->login($username,$pass);
                  my @out=$t->cmd("copy start tftp");
                  my @out=$t->cmd("xxx.xxx.7.90");
                  my $out=$t->cmd("$station-router-confg");
                  print "downloading config...\n";
                  my $out=$t->cmd("");
                  $t->close;
}

And here a sample from "routerlist.txt"
a20-2509-router
a30-2509-router
a40-2509-router
a50-2509-router
a60-2509-router
a70-2509-router
a75-2610-router
a80-2509-router
So if you have a single password for all your switches and you always want to work off a list, you can just specify them like so. If you don't have a username, just don't put it in $t->login. If you have multiple passwords, put them on the same line as the router name (or IP) and use the split function to grab it.

$pass="password";
$enable="enable";
$username="[put a username here]"; # login name if needed
$infile="routerlist.txt"; # list of routers to read from
#
use Net::Telnet::Cisco;
open (LIST,"$infile") or die "Couldn't open file $infile";
     @list=<LIST>;
     foreach (@list) {
               ($host)=split(/\s+/,$_);
               &TELNET;
               }
}
     
sub TELNET {
     $t = Net::Telnet::Cisco->new(
          Timeout => 10,
          Host    => $host
               );
     $t->errmode("return");
     print "Trying $host\n";
     $t->login($username,$pass);
               my @out=$t->enable($enable);
               my @out=$t->cmd("copy start tftp");
               my @out=$t->cmd("xxx.xxx.7.90");
               my $out=$t->cmd("$station-router-confg");
               print "downloading config...\n";
               my $out=$t->cmd("");
               $t->close;
}
Avatar of Fubyou

ASKER

mikebernhardt

Cisco Works for Windows 6.1 has the ability to automate this?? How exactlly is that.
Ciscoworks should be configured to automatcially tftp all of your device startup and running configs at regular intervals and upon changes, and archive them. You can view them any time. The Campus Manager can be used to look at MAC addresses, IPs, etc. on your switches.
The RME module does all of your config management and downloading. I don't believe it's any different on Windows or Solaris.

CiscoWorks for Windows does not have the capability to automate that function. It is in the RME portion of CiscoWorks 2000.

You can try an application like Kiwi CATtools...
http://www.kiwisyslog.com/cattools2.htm

I like 1.x version better than the 2.x version, but that's just personal preference..


Well that sucks...

With perl as described above, you can get anything you want out of a router or switch. Of course, you have to learn a little perl... but if you have any scripting experience it's well worth it.  FYI, the above script pulled down 40-some-odd configs in like 5 minutes.
Avatar of Fubyou

ASKER

Give me a couple days to award points im going to test the perl code you left. I pretty sure that perl is the right answer. I've done alot of cool programs in perl its a very powerful scripting language.
ASKER CERTIFIED SOLUTION
Avatar of mikebernhardt
mikebernhardt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've used the telnet scripting tool to get the configsfrom lots of switches and routers saved on a regular basis as you can automate it by adding it to your scheduled tasks. You can get it here
http://www.freewareweb.com/cgi-bin/archive.cgi?download=1&ID=645
Simple to use and set up but if you have any problems let me know.
Trying out the scripting took Sibleyc and having a few probs with it.  Seems that whenever it send the text to the screen it is duplicating the text, like local echo is turned on or something.  Have you seen this happen before?
Mikebernhardt,
Can you look here? We can use your expertise to create a telnet script for a PIX:
https://www.experts-exchange.com/questions/21495231/Internal-IP-Failover-Cisco-PIX-501.html#14470973