Hello again my fellow network admins, wondering how to automate your daily, weekly or monthly tasks you have for your network devices? In case you don’t have a management tool that can do config backups for your network devices. You can use “expect” which is the best thing in the opensource, one package i know which supports “expect” is tcl. You can google active tcl, which has a binary for windows. Ok, so if you have your tcl installed in your PC. We’re ok to proceed with our easy to script. A notepad will be our first step to create our script, for windows code we need to have this to declare first before the rest of the code:
#!/bin/sh
#\
exec tclsh “$0″ ${1+”$@”}
package require expect
Note: Failure to declare package require expect statement will result an error message in windows platform.
Next to orgranize our code by setting up variables. (But its ok not to have this its just my way of doing it).
set name “variable”
set hostname “192.168.1.1″
set username “user”
set password “password”
set enapwd “password”
Now this code is just really like a show and tell script, we’ll teach our code what to expect on its prompt and what to send to execute on our command line.
So after we have set all the variables needed we can now call the terminal or the telnet applications by issuing the spawn in our code
spawn plink -telnet $hostname
So if you guys wondering what’s plink is, it’s a command line application of putty, i used this instead of the windows telnet because its more accurate and does not show ascii characters when I run the script. You can try it though if those things shows up in your script.
expect “sername:”
send “$username\r” #\r is the return command after the variable we set. Don’t forget to put that on every send command on the script. Else it will just sit on your prompt and won’t do anything.
expect “assword:”
send “$password\r”
expect “>”
send “enable\r”
expect “sername:”
send “$password\r”
expect “router#”
send “terminal length 0\r” # you can set your device (ex cisco) to have a no page breaks when you issue your show run or show start. The script can’t do anything to send “press spacebar or return key”.
expect “outer#” #notice why i omit the first letter of the device prompt, it seems when i have the same prompt on my expect the command does not work. Feel free to experiment on your own on this to see different outputs.
send “show run\r”
expect “uter#”
send “terminal length 24\r”
expect “ter#”
send “exit”
eof #it states that the script is done
So that’s it guys, feel free to leave a meebo message for comments and suggestions. You can do a lot of things in this script aside of doing config backups. ciao =)
Filed under: Network Admin | Tagged: automatic backups, expect, Network Admin, scripts, tcl



very interesting, but I don’t agree with you
Idetrorce