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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting EXPECT: Assign variable by reading a line of text from a file
# 1  
Old 10-14-2012
Question 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.

Code:
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.


Code:
#!/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 Smilie) autokey script which reads from a file:

Code:
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}

}

# 2  
Old 10-17-2012
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading foreach variable from a text file

Hello, I have a text file named "foreach.txt" which reads like --- foreach cal ( 1 2 3 4 5 ) I am using a simple script which looks like --- #!/bin/tcsh foreach cal (1 2 3 4 5) echo "$cal" end Is it possible to modify the script in such a way that instead of writing foreach cal (1... (6 Replies)
Discussion started by: Indra2011
6 Replies

2. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

3. UNIX for Dummies Questions & Answers

Assign line from file to variable

Hi, I am new to shell scripting. Need help with the below requirement. I need help to read a log file and line containing word ORA needs to be captured into a variable and the values of the variable need to be inserted into a table. For E.g. file test.sql has below error: ORA-01017:... (3 Replies)
Discussion started by: ricsharm
3 Replies

4. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

5. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

6. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

7. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

8. Shell Programming and Scripting

reading line by line from a text file

Hi, I have a text file something like this: 10.10.10.1, ldap, cn=users,dc=example,dc=com ..... ... and many more lines ... ... now i want to read each individual line from the file and assign it to a variable example: the script should read 10.10.10.1 and assign it to a variable say... (3 Replies)
Discussion started by: sunrexstar
3 Replies

9. Shell Programming and Scripting

Perl:Read single value from text file and assign to variable

Hello All, A part of my very basic perl code requires me to read a single value from a text file. The file output is the following: Reading image ... done IMAGEREGION=0x0x0-256x162x256 VOXELDIMENSION=0.9375000000x1.2000000477x0.9375000000 VOXELNUMBER=10527001... (7 Replies)
Discussion started by: ncl
7 Replies

10. Shell Programming and Scripting

reading the whole line from a file into a variable

Hi, I am doing : while read line do printf "%s\n" ${line} done <datafile.txt but I am not getting each single line from the data file assigned to the variable line (but only tokens/fields at a time). I also tried while IFS= read -r lineI want the whole line assigned or read into the... (2 Replies)
Discussion started by: shri_nath
2 Replies
Login or Register to Ask a Question

Featured Tech Videos