cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
16666
Views
5
Helpful
4
Replies

Using Cisco Commands in a Batch file

majdalani
Level 1
Level 1

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. ?

2 Accepted Solutions

Accepted Solutions

cpubob
Level 1
Level 1

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

View solution in original post

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

View solution in original post

4 Replies 4

cpubob
Level 1
Level 1

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

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 ?

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

thank you

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Review Cisco Networking products for a $25 gift card