3

I want to use expect file for connecting to telnet session automatically, the flow is:

  • when we connect to some IP & port, it will show escape character is ]
  • after that we have to give Ctrl+D

I wrote one expect file that sends \x04 for Ctrl+D. When I run that file it is fine for connecting but expecting me to give Ctrl+D from keyboard. But I want it to automatically enter Ctrl+D from the script itself. Nothing should be done manually.

Can you please explain the way?

  • 4
    Did you mean to send \x04 (with a backslash)? I'd recommend starting with autoexpect and reviewing the generated script. – glenn jackman Dec 19 '12 at 19:08
2

You can auto exit from telnet without using expect shell.

sleep <n> | telnet <server> <port>

n - The wait time in seconds before auto exit. It could be fractional like 0.5. Note that some required output may not be returned in the specified wait time. So we may need to increase accordingly.

server - The target server IP or hostname.

port - Target service port number.

You can also redirect the output to file like this,

sleep 1 | telnet <server> <port> > output.log
1

I would suggest to change the escape character to one you can send easier in expect. For example, if you know you're not sending the exclamation mark, use it as the escape character:

$ telnet -e ! 10.1.2.3 80
Telnet escape character is '!'.
Trying 10.1.2.3...
Connected to 10.1.2.3.
Escape character is '!'.
GET!
telnet> quit
Connection closed.
  • You can use send \x1d to send ^[ aka ctrl + [ – KCD May 8 '13 at 23:49
0

You can type ctrl+V,ctrl+D to get the literal byte sequence in your expect script. Expect will then send this exactly as if you interactively typed ctrl+D.

  • Thanks for replying. I tried <kbd>Ctrl</kbd>+<kbd>V</kbd>, <kbd>Ctrl</kbd>+<kbd>D</kbd> but it didn't work. We are defining Escape character is ] in the script. This is predefined for us; we can't change it. Can you suggest any other ways that will help me out. – user29300 Dec 26 '12 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.