EXPECT: Assign variable by reading a line of text from a file

Hi All,

I have been using a program on windows called AutoKey.
My environment at work is Linux and I have been experimenting with expect. Very powerful. I can move my AutoKey scripts to Linux using Expect once I am educated on how to read from a file using Expect.

My application would be as follows:

  1. I have a list of MAC addresses in a text file format aa:bb:cc:dd:ee:ff. There will be one MAC address per line.
  2. Expect script will need to read the first mac address in the file. Carry out some work with that MAC and then read the next MAC address and so on until the end of the file.

I have a script which works fine as follows, but the MAC is prompted by the user. This is the part which needs to be automated and the MAC address read from the line in the file so the MAC read from the file is the variable for.

set mac $expect_out(1,string)

I would appreciate some guidance on how to use expect to read the first line of a file, carry out the work needed with the MAC, and then loop back and get the next MAC until the end of the list.

#!/usr/bin/expect -f
log_user 0
set force_conservative 0  ;# set to 1 to force conservative mode even if
			  ;# script wasn't run conservatively originally
if {$force_conservative} {
	set send_slow {1 .1}
	proc send {ignore arg} {
		sleep .1
		exp_send -s -- $arg
	}
}

set timeout -1

# grab the base and mac address
send -- "\n"
send -- "\n"
send_user -- "Basestation: "
expect_user -re "(.*)\n"
send_user "\n"
set base $expect_out(1,string)

send_user -- "Full MAC address with colons: "
expect_user -re "(.*)\n"
send_user "\n"
set mac $expect_out(1,string)
spawn telnet $base
send -- "com21\r"
send -- "*************\r"
send -- "2\r"
send -- "1\r"
send -- "5\r"
send -- "1\r"
send "$mac\r"
send "\r"
send "\r"
interact

This is Windows (sorry :eek:) autokey script which reads from a file:

Loop 80 {
Send, 1{enter}
sleep, 800
FileReadLine, line, C:/vlan122.txt, %A_index%
Send, %line%{enter}
sleep, 200
Send, {enter}
Send, True
Sleep, 200
Send, {enter}
Send, 122
Send, {enter}
Send, {enter}
Send, {enter}
Send, {enter}
Send, {enter}
Send, {enter}
Send, {enter}
Send, {enter}
Send, {enter}
sleep, 200
Send, {enter}
Send, {enter}
Send, {enter}
Send, y
Send, {enter}

}

Expect is for interactive keyboard driven tools with no batch or script-friendly command line mode. If you make an expect script that works with one mac address, you could write a normal bash/ksh script to read the mac addresses, gen an expect script with that mac address embedded and run it. Expect is written in tk/tcl, as I recall, so you need skills in that arcane area to vary the script dynamically. I mostly write my expect scripts using autoexpect, or fake it using shell scripts reading the output file to determine progress. Are you going to windows autokey using telnet?