Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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

3 Answers 3

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.
share|improve this answer
    
You can use send \x1d to send ^[ aka ctrl + [ –  KCD May 8 '13 at 23:49

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
share|improve this answer
    
Interesting method. Thanks for sharing. –  0xC0000022L Jun 13 at 12:42

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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