how do I sent escape character ^] through a spawned telnet session?
Asked Answered
P

3

7

I am able to open a spawned telnet session in a shell script to run some commands. Afterwards, I am trying to send the escape character using send (see syntax below) but it never gets to the part where I am able to close the session. Can someone point me in the right direction? Due to this issue, I am opening up multiple telnet sessions this way, which I do not want to do.

send "^]\r"
expect "telnet>"
send "close\r"
close $spawn_id 
Prase answered 16/10, 2013 at 18:55 Comment(1)
donsnotes.com/tech/charsets/ascii.html#cntrlPrase
R
10

You send the ESC control sequence. Try send "\x1b\r"

Responsion answered 16/10, 2013 at 19:25 Comment(3)
Thanks for your response...it pointed me in the right direction. I found that x1d\r prints ^] which is what I needed.Prase
Is \r really necessary?Hemangioma
@Hemangioma It depends on what you want to send afterwards. So, no. :-)Albright
D
2

I was trying to solve this question since the past couple of days and finally found a solution :)

The \x1b\r and \x1d\r did not work for me. I found the solution here:Using Control character in a script with tr and echo

I used the following code:

escseq=`echo 'e' | tr 'e' '\035'`

Use $escseq to get Ctrl+].

How this works:

If your echo can't make control characters directly, the tr utility can do it for you. tr understands octal sequences, too. Make your echo output characters you don't want, and have tr translate them into the control characters you do want.

For example, to make the 4-character sequence ESCape CTRL-a [ CTRL-w , use a command like this:

escseq=`echo 'ea[w' | tr 'eaw' '\033\001\027'`

tr reads the four characters down the pipe from echo ; it translates the e into ESCape (octal 033), the a into CTRL-a (octal 001), and the w into CTRL-w (octal 027). The left bracket isn't changed; tr prints it as is.

Note: This is extremely useful if you want to automate logout of a telnet session using a shell script.

Delastre answered 4/2, 2014 at 19:11 Comment(1)
I tried this one and it works great for me. In expect, do set escseq "echo 'e' | tr 'e' '\035'"Rennes
O
1

CTRL+R represents the "^]" escape sequence to get out of telnet

Ouphe answered 8/3, 2018 at 15:20 Comment(2)
This does not answer the question. The OP is not able to send CTRL+R programatically, whereas the existing accepted answer actually provides a solution.Pectoralis
Realized that soon after posting my answer. I wanted the answer to be here nonetheless if someone accidentally finds it useful for another reasonOuphe

© 2022 - 2024 — McMap. All rights reserved.