Solved

Expect question.  Should be a simple one.

Posted on 2015-02-06
8
129 Views
Last Modified: 2015-02-07
I have the following code, which I would think would be simple, but then again, I'm just trying to figure expect out:

#!/usr/bin/expect -d
set ZERO [lindex $argv 3]
set ONE [lindex $argv 4]
set TWO [lindex $argv 5]
set THREE [lindex $argv 6]
set timeout 360

send_user "$ZERO\r"
send_user "$ONE\r"
send_user "$TWO\r"
send_user "$THREE\r"

I get the following when I run it.  Why don't I get the variables sent to standard output?

testbox-[root]/home/share/daldrid/work/aix/rootpw> exptest.exp test1 test2 test3 test4
expect version 5.39.0
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4
set argc 4
set argv0 "exptest.exp"
set argv "test1 test2 test3 test4"
executing commands from command file exptest.exp
testbox-[root]/home/share/daldrid/work/aix/rootpw>

Thanks in advance!
David
0
Comment
Question by:David Aldridge
8 Comments
 
LVL 1

Author Comment

by:David Aldridge
Comment Utility
How about a link to some good, understandable expect tutorials?
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 125 total points
Comment Utility
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 125 total points
Comment Utility
It probably is being sent, but your send_user is sending the text, then a carriage return to put the cursor back at the start of the line - eash send_user then overwrites the previous one, and the last send_user output is overwritten by the shell prompt!

To fix, change all of the "\r" at the end of send_user strings to "\n".

You *do* use "\r" when using "send" to send data to a spawned process.
0
 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 250 total points
Comment Utility
Yes, the variables are  being output. I ran exptext.script under script which captures all terminal I/O, and I can see (some of) them.

Will continue to investigate
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 34

Expert Comment

by:Duncan Roe
Comment Utility
Your code does not give correct output, but it does give more than you realized
22:01:11$ script exptext.script
Script started, file is exptext.script
22:01:19$ ./exptest.exp test1 test2 test3 test4
expect version 5.44.1.15
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4  
set argc 4
set argv0 "./exptest.exp"
set argv "test1 test2 test3 test4"
executing commands from command file ./exptest.exp
22:01:31$ exit
exit
Script done, file is exptext.script
22:01:54$ less exptext.script
Script started on Sat 07 Feb 2015 10:01:19 PM EST
22:01:19$ ./exptest.exp test1 test2 test3 test4
expect version 5.44.1.15^M
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4  ^M
set argc 4^M
set argv0 "./exptest.exp"^M
set argv "test1 test2 test3 test4"^M
executing commands from command file ./exptest.exp^M
test4^M^M^M^M22:01:31$ exit
exit

Script done on Sat 07 Feb 2015 10:01:54 PM EST
22:02:02$ 

Open in new window


expect is smart enough to not include its option switches or its file argument in the argv list. Also, argv[0] is put in its own variable, so you want to print the argv list starting at element 0.
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 250 total points
Comment Utility
Here's a minimally changed version of your script, which runs correctly without needing script to see the output
22:16:42$ cat exptest.exp
#!/usr/bin/expect -d
set ZERO [lindex $argv 0]
set ONE [lindex $argv 1]
set TWO [lindex $argv 2]
set THREE [lindex $argv 3]
set timeout 360

send_user "$ZERO\r\n"
send_user "$ONE\r\n"
send_user "$TWO\r\n"
send_user "$THREE\r\n"
22:18:25$ ./exptest.exp test1 test2 test3 test4
expect version 5.44.1.15
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4  
set argc 4
set argv0 "./exptest.exp"
set argv "test1 test2 test3 test4"
executing commands from command file ./exptest.exp
test1
test2
test3
test4
22:18:30$ 

Open in new window

0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
Suggestion: looks like you're learning expect while being logged on as root? I'd try and test things as an ordinary user to prevent unexpected behavior ;)
0
 
LVL 1

Author Comment

by:David Aldridge
Comment Utility
Some great advice.  I'll split this up for you guys.   Thanks for the help!  As for running it as root, I'm playing around on a Solaris zone that I can blow away or just restore a snapshot for it.  Not real data on it.


Thanks again!
David
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now