Using Cisco Commands in a Batch file

Answered Question
Jan 29th, 2008

Hi,

Does any one know how to write a batch file to configure cisco switches.

for example i have 20 2970 switches, and i dont want to enter each one by one to change the password.

the lines are simple:

telnet 172.16.0.1

password

en

password

config t

line vty 0 15

password mypassowrd

login

does anyone know how to include this in a batch file, so that i can telnet each switch and run these commands. ?

I have this problem too.
0 votes
Correct Answer by cpubob about 6 years 2 months ago

its more of a sample to do whatever you want.

expect works like a chat script, you do something and then you evaluate what you got back. You can use it to do whatever commands you want, you just have to add some more line to the script. The basis of the script I sent was to give you an idea of what you can do. Every command on the router gives you something and then returns you to the prompt, on the router in enable mode, the prompt ends with a #. So the # is what I set the expect script to expect back. Basically your saying, do a command and wait till you see a # on a line, that means your done with that command and you can now enter another command.

I used variables in the script so that you can use the script on devices with different passwords without having to rewrite the script every time. Again, just to give you an idea of how it works.

If you had a file of ips, it would be a plain text file, one ip per line, like this:

x.x.x.1

x.x.x.2

x.x.x.3

x.x.x.4

nothing fancy, just text.

The script I attached just changes the vty password, but if you want to change the enable password, just follow the script to somepoint where you'd be in global config mode (the mode your in after you type "config t" ) and then add a few lines that change the enable password. expect is very flexible and pretty easy to learn. Just for your reference, expect is based on TCL so if you want to add loops or an if statement, google "tcl if" or get a tcl book.

GL

Correct Answer by cpubob about 6 years 2 months ago

use expect

the attached file would be run from a box that had expect installed (either *nix or windows) and you would call it with something like this:

expect telnet.expect 10.1.1.1 pass enablepass newpass

If you had a text file with all the switch ip addresses then you could write a script to do all your changes. Assuming your file with the ip's is called switches.txt:

for i in $(cat switches.txt); do expect telnet.expect $i pass enablepass newpass ; done

HTH

-Rob

  • 1
  • 2
  • 3
  • 4
  • 5
Average Rating: 5 (2 ratings)
Correct Answer
cpubob Tue, 01/29/2008 - 09:18

use expect

the attached file would be run from a box that had expect installed (either *nix or windows) and you would call it with something like this:

expect telnet.expect 10.1.1.1 pass enablepass newpass

If you had a text file with all the switch ip addresses then you could write a script to do all your changes. Assuming your file with the ip's is called switches.txt:

for i in $(cat switches.txt); do expect telnet.expect $i pass enablepass newpass ; done

HTH

-Rob

Attachment: 
majdalani Tue, 01/29/2008 - 12:22

Hi thanks for the reply.

1- is this file only for changing the enable password ? does it work also for the telnet password?

2- how the format of the switches.txt file would look like ?

Correct Answer
cpubob Tue, 01/29/2008 - 12:37

its more of a sample to do whatever you want.

expect works like a chat script, you do something and then you evaluate what you got back. You can use it to do whatever commands you want, you just have to add some more line to the script. The basis of the script I sent was to give you an idea of what you can do. Every command on the router gives you something and then returns you to the prompt, on the router in enable mode, the prompt ends with a #. So the # is what I set the expect script to expect back. Basically your saying, do a command and wait till you see a # on a line, that means your done with that command and you can now enter another command.

I used variables in the script so that you can use the script on devices with different passwords without having to rewrite the script every time. Again, just to give you an idea of how it works.

If you had a file of ips, it would be a plain text file, one ip per line, like this:

x.x.x.1

x.x.x.2

x.x.x.3

x.x.x.4

nothing fancy, just text.

The script I attached just changes the vty password, but if you want to change the enable password, just follow the script to somepoint where you'd be in global config mode (the mode your in after you type "config t" ) and then add a few lines that change the enable password. expect is very flexible and pretty easy to learn. Just for your reference, expect is based on TCL so if you want to add loops or an if statement, google "tcl if" or get a tcl book.

GL

Actions

Login or Register to take actions

This Discussion

Posted January 29, 2008 at 9:03 AM
Stats:
Replies:3 Avg. Rating:5
Views:2565 Votes:0
Shares:0
Tags: No tags.

Discussions Leaderboard

Rank Username Points
1 15,007
2 8,150
3 7,730
4 7,083
5 6,742
Rank Username Points
155
77
70
69
50