Send spawn id exp4 not open error in expect

expecttcltelnet

My ultimate goal is to do telnet to a router and interact with it. The following expect script does not help me at all. The router does not have any user id or pasword. it is automatic login.

Expect file code.

set iptotelnet "10.x.x.x"
spawn telnet $iptotelnet

sleep 10
expect ">" #this is because initially the prompt will be >
sleep 10
send "enable\r" # this should change the prompt from > to # 

sleep 10
expect "#"
sleep 10
interact
sleep 10
exit

However, I get the following error.

send: spawn id exp4 not open
    while executing 
send "enable\r".

This is not just with telnet, i get the same error with any other command also.

please help me.

Best Answer

Your script has no major issues with it, not that would cause spawn to fail like that. (The comments you have would cause problems, but are trivially fixable by using ;# instead of #.) Therefore your problem lies elsewhere (well, with very high probability).

I see that you are trying to control telnet with Expect on Windows. Alas, telnet is a special case that can't be controlled this way — Expect on Windows uses the system debugging facilities to intercept terminal output, but this doesn't work for executables that have special system permissions set, and telnet is one of the programs for which this is true — so you need another approach. The simplest is to get plink.exe (which is really PuTTY for terminals/automation) and to use that (in “telnet” mode) instead of telnet.

Related Topic