Calling Expect Script - Telnet

Hi All,

I have an Expect script which logs into Cisco switch, performs a show interface command. I want to read a file of ip addresses which will be passed to the expect script.

The script to read the file works, the expect script works on it's own but when i call the 'expect' script from the control script it fails with : : spawn: not found

I've looked at many threads on many forums but unable to work this one out, any help would be appreciated.

Script1: Control Script:

#!/bin/bash
#!/usr/bin/expect -f
#
#
while read  ipadd
do
echo $ipadd
export ipadd
sh ./list-int $ipadd
done < dev-list

Script2 : Expect

#!/usr/bin/expect
#!/usr/bin
# Variables #
#set device "10.33.220.12"
set device $1
set user ****
set cmd1 "term len 0"
set cmd2 "show ip int brief"
set logout "exit"
set pass ****
set timeout 100

#TELNET
spawn telnet $device
expect "Username:"
send "$user\n"
expect "Password: "
send "$pass\n"
expect ">"
send "en\n"
expect "Password: "
send "$pass\n"
expect "#"
sleep 2
send "$cmd1\n"
expect "#"
end "$cmd2\n"
expect "#"
send "$logout\n" 

Please remove the expect line from the control script and it will/should work.

Thanks for quick response dude2cool, have done as suggested but same error i'm afraid.

so when you run it manually giving it an IP argument it works, right?

sh ./list-int 10.x.x.x

where list-int is your expect script.

In your control script, modify #!/bin/bash to #!/bin/bash -x, this should enable tracing. Post the output from the run with trace output. If it is a lot of output, put in a file and attach to your post.

Correct, running with manual IP argument works.
Output from run with trace :

/Scripts$ ./get-int
+ read ipadd
+ echo 10.33.220.12
10.33.220.12
+ export ipadd
+ sh ./list-int 10.33.220.12
./list-int: 17: spawn: not found
couldn't read file "Username:": no such file or directory
./list-int: 19: send: not found
couldn't read file "Password: ": no such file or directory
./list-int: 21: send: not found
couldn't read file ">": no such file or directory
./list-int: 23: send: not found
couldn't read file "Password: ": no such file or directory
./list-int: 25: send: not found
couldn't read file "#": no such file or directory
./list-int: 28: send: not found
couldn't read file "#": no such file or directory
./list-int: 30: send: not found
couldn't read file "#": no such file or directory
./list-int: 32: send: not found

and repeat for every entry in dev-list file.

Thanks again....

What is line number 17 of your expect script? it is failing on that.

Yes, that's the error I posted originally. It's the spawn telnet $device command which works when run with manual IP but fails when called from control script.

Can you try the following. Edit your expect script and remove #!/usr/bin line, not sure what it is doing in your expect script and change #!/usr/bin/expect to #!/usr/bin/expect -f

See if that makes any difference.

And from your control script, instead of calling it like sh ./<expectscript> ipaddr, call it like this:

/usr/bin/expect <expectscript> ipaddr

Something like this:

/usr/bin/expect /pathtoyouscript/list-int $ipadd

OK, first change didn't accomplish anything.
Second change, calling via /usr/bin/expect<expectscript> ipadd, with trace running outputs:

~/Scripts$ ./get-int
+ read ipadd
+ echo 10.33.220.12
10.33.220.12
+ export ipadd
+ /usr/bin/expect list-int 10.33.220.12
can't read "1": no such variable
    while executing
"set device $1"
    (file "list-int" line 6)

So changing the method of calling the script has stopped the argument being passed by the looks of it...

One more change, in your expect script change your set device $1 to

set device [lindex $argv 0]

Keep all the other change from above intact.

Your are star, that's solved it. obviously i need more research on passing arguments !!

Many thanks again, superb...

Glad I could be of some help. Have fun with expect, it is a great tool.

Best Regards to you...Thank You.