2

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Alapati
  • 135
  • 1
  • 5
  • There's nothing obviously wrong with the script (except for the comments, which should either be on separate lines or introduced with `;#` instead of `#`). I'm guessing that the problem is down some configuration issue. – Donal Fellows Sep 14 '12 at 12:19

3 Answers3

2

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Notes: You can't automate PuTTY itself like this; it is GUI subsystem application so you'd need a GUI automator, and they're _far more complex_ than Expect. You also can't compare the experience of Windows in this area with Unix; they handle terminals in totally different ways (Expect/Unix uses the system virtual terminal facility, which doesn't exist on Windows at all). – Donal Fellows Sep 14 '12 at 12:29
  • Yes..I understand whatever u said. So, Is there a different approach for me? because, i have to work on windows...(company rules). – Alapati Sep 14 '12 at 12:56
  • can u please give some more details on using plink ? some links atleast..i googled but could not find anything useful...please help me. – Alapati Sep 14 '12 at 16:47
0

Probably you can't use # comments in send command line.
Try same but without comment in send string.

Laser
  • 6,652
  • 8
  • 54
  • 85
  • I added comments for readability sake only. Initially there were no comments. I tried it without comments too.. Not working... – Alapati Sep 14 '12 at 10:39
  • I think spawn can't work with `telnet 10.x.x.x` command. Did you try it manually? – Laser Sep 14 '12 at 10:47
  • Ya...I tried manually from command prompt and it worked. Seems like spawn and telnet don't work in windows xp. Is there a work around? – Alapati Sep 14 '12 at 11:05
  • Hmm, what version of expect you are using and where you get it? – Laser Sep 14 '12 at 11:09
  • I installed expect from Cygwin. Expect version is 5.45 – Alapati Sep 14 '12 at 11:11
  • I searched somewhere and found this thing: spawn putty -telnet xx.xx.xx.xx will work. However, it opens a new putty terminal window and does not follow up with the remaining commands in the expect file. – Alapati Sep 14 '12 at 11:11
  • Hmm it's work fine for me. I install telnet to cygwin from "inetutils" package. And all works fine, but without commentaries in code. – Laser Sep 14 '12 at 11:20
  • Remove comments, download telnet from inetutils package. Made scripts with first line #!/usr/bin/expect. Try to run it. – Laser Sep 14 '12 at 11:42
  • I am getting a linux based package. How do I install it? – Alapati Sep 14 '12 at 11:58
  • Install package inetutils from cygwin.exe. – Laser Sep 14 '12 at 12:00
0

It happens to me too. And it has been resolved.

I try to automatically telnet a server in the Cygwin on Windows.

But the Microsoft telnet installed in Windows won't work in a Cygwin shell

If you got the response below, this is the source of your problems type: $ which telnet /cygdrive/c/WINDOWS/system32/telnet

There are two solutions: 1. For Cygwin x86, just simply install the package "inetutil" which includes telnet command. 2. For Cygwin x86_64, currently(2013-09-09) the "inetutil" hasn't been port to 64bit, so I use the plink.exe which is part of PuTTy instead

superlee
  • 826
  • 8
  • 5