1834620 Members
2508 Online
110069 Solutions
New Discussion

expect

 
SOLVED
Go to solution
jerry1
Super Advisor

expect

Does anyone know how to pass args from a file
to an expect script or using autoexpect.

Just need to do a:

for i in ;do
telnet $i


done

But pass the other commands also.
How would you get autoexpect to do this or
just in an expect file?

Would anyone have a similar example that I
could modify?
5 REPLIES 5
Sridhar Bhaskarla
Honored Contributor
Solution

Re: expect

Hi,

In expect you can use argc and argv to capture the arguments.

Look at 'passmass' example under wherever_expect_is/bin directory if you already have expect installed.

...
for {set i 0} {$i<$argc} {incr i} {
set arg [lindex $argv $i]
switch -- $arg "-user" {....}
...
set host $arg
...

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Michael Tully
Honored Contributor

Re: expect

Attached is an example I use to start a command that requires a password. No password is echoed to the screen, just picked up from a file.
Anyone for a Mutiny ?
A. Clay Stephenson
Acclaimed Contributor

Re: expect

As a Plan B, you might consider using Perl's NET::Telnet module. It can login automatically using .netrc, if you like, and you get expect-like pattern matching for free. Man Net::Telnet for details and several useful examples. Even if you don't know Perl, the examples will all but get you through it.

If it ain't broke, I can fix that.
jerry1
Super Advisor

Re: expect

What I am trying to do is have a file with



.
.

That I have to use to telnet to the jetdirect cards and reset the IP and Gateway addresses. I have over 200 printers that have to be done so manually going to the printers would take about 60 hours or having to telnet to each printer manually and manually changing them would be just about as bad.

Since I can create an autoexpect script to do a telnet . I could not figure out how to pass each line of input from the file to the expect script.

Thanks to all for your input.
I will checkout the info you posted.

jerry1
Super Advisor

Re: expect

This is what I was able to come up with but
it continues to loop for only the first line in the data file. Its not going to the next entry.

This is connecting to an HP jetdirect card so the expects are okay and it works on the first one from the data file but does not get the next enry. Something wrong with the "while" statement.

Can anyone help?


#!/opt/expect/bin/expect -f

set dfile "./testdata"

set file [open $dfile r]
gets $file line
while {![eof $file]} {
spawn telnet $line
expect "> "
send "/\r"
expect "> "
send "exit\r"
expect "Connection closed by foreign host.\r"
}

expect eof