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?

ctype.h
  • 556
  • 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 Answers3

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
Seff
  • 338
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.
gertvdijk
  • 13,977
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.

bahamat
  • 39,666
  • 4
  • 75
  • 104
  • Thanks for replying. I tried Ctrl+V, Ctrl+D 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. –  Dec 26 '12 at 18:22