Thursday, August 13, 2009

Using "expect" in Tcl

Here's a guide on how to use "expect" in a Tcl script, i've created a script to get the running-config of a router using expect as an example. 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”

NOTE: this will not work in 64-bit OS. You'll get an error "Can't find package expect".

So that’s it guys, feel free to leave a comment for suggestions. You can do a lot of things in this script aside of doing config backups. ciao =)

2 comments:

  1. Thanks for sharing the info ..You have told as how to use from windows ..I need help on running in Unix environment .I have an issue as below:
    I have a Tcl script which calls the expect script and which in turn spawns a telnet command. When i run the expect script separately it works..but when i try from Tcl script it does not recognize the spawn command.
    Pls help me on this

    ReplyDelete
  2. Do you still have this in your script "exec tclsh “$0″ ${1+”$@”}"? If yes remove it, this is for windows only.
    This should suffice if its a unix script.
    #!/usr/bin/expect

    ReplyDelete