01-29-2008 09:03 AM - edited 03-05-2019 08:47 PM
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. ?
Solved! Go to Solution.
01-29-2008 09:18 AM
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
01-29-2008 12:37 PM
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
01-29-2008 09:18 AM
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
01-29-2008 12:22 PM
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 ?
01-29-2008 12:37 PM
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
07-27-2020 05:51 AM - edited 07-27-2020 05:53 AM
thank you
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide