Monday, January 10, 2005

Automating Telnet with Expect

As long as Telnet has been around, I was suprised to find that there is no native way to automate it.

I need a process on a Wintel box to access a TSX system remotely and kick off a process. Manually I can Telnet to the box, provide user/pass and then start the process. But when trying to automate the process I find there is no native way to respond to a password prompt from Telnet.

Enter "Expect". Written for *nix in TCL there is a port that runs on Wintel. The documentation is a bit sketchy, but I got it to work.

You can call Expect with -f and provide an input file. The input file for the whole process looks like this:

spawn telnet
expect "Logon please:"
send "myusername\r"
expect "Password:"
send "mypassword\r"
expect "32sys>"
set timeout 1000
send "c:\\bin\\myscript\r"
expect "32sys"

The \r is the return control, notice double back-slash for the directory name. The package comes with TCL source and its own version of telnet. The expect "32sys" lines have expect looking for the command line prompt.

Whatever you are calling on the remote side should have limited output, when working with TSX I get a minimum of 2 lines of control characters that look like garbage. Originally the remote process was reporting progress on exporting data... this made for too much information for Expect to ... well, expect.


WilliamSanders said...

ouch. been doing rsh, rexec, and telnet encapsulation with vfp for about 6 years now. Did you look at other tools? Even got stuff to work with screen scraping on these controls with vfp. TSX is cool - are you doing anything with TSX-32 ? see for more info - its a great beastie to use when supporting FPD apps over the internet.

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!