Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expect script for telnet automatization

Status
Not open for further replies.

halaster

Programmer
Aug 26, 2009
1
DE
Hello everyone.

I am currently trying to learn how to write Expect scripts. Unfortunately, the web resources on the topic are a little vague for the most part, and tcl language reference usually don't include Expect-specific issues. I've also read the man page, but again, no avail. So I hope you can help me, or at least point me to someplace I can get help.

The script is supposed to do the following:
- connect to a telnet server running a program which responds to certain messages with reports on the status of the telephony application running on the server
- pass such messages to the server
- read the output and use it to generate another message containing this output

The messages are as follows:
Connect to the application
init
passwd=
delimiter=;
<empty line>

This returns:
result
ok
ok
<empty line>

Find a call being placed by a certain user
get
sub=X
<empty line>
where X is the SIP username of the user.

This returns:
result
ok
ok
ID;X; <lots of irrelevant data>
<empty line>

Record the call:
record_call
on
ID;X
<empty line>

As you can see I must read the ID from the result, in order to initialize the second message, since every call has a unique ID.

Now, with my limited tcl/Expect knowledge, I produce the following script:
#!/usr/bin/expect

spawn telnet 192.XXX.XXX.XXX XXXX
expect "Escape " {send "init \r"; send "password=\r"; send "delimiter=;\r"; send "\r"}
expect "ok\r" {expect "\r" {send "get\r"; send "sub=601\r"; send "\r"}}
expect { ";601" {set result $expect_out(0,string); send "record_call"; send "on"; send $result; send "\r"}
"ok" {puts stdout "Kein laufender Anruf!"}
timeout {set result "Kein Ergebnis!"}
}
puts "The result is '$result'."

The first and second step execute fine, the script connects to the application and returns the data for the call. But then it times out with no return value at all, and informs me that $result has not been set.

What is my mistake? I know that the program is using a lot of empty lines and I feel like this might be causing the exp_out buffer to look funny, but I can't tell for sure. BTW, I used several variants there, namely $exp_out(buffer) and various places in the buffer.

CU,
André

PS: If this was the wrong forum to post this question in, please just say so.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top