How do I send the escape character ^] via the generated telnet session?

I can open a spawned telnet session in a shell script to run some commands. Subsequently, I try to send an escape character using send (see the syntax below), but it never ends up in the part where I can close the session. Can someone point me in the right direction? Due to this problem, I open several telnet sessions in a way that I do not want to do.

send "^]\r" expect "telnet>" send "close\r" close $spawn_id 
+6
source share
3 answers

You are sending an ESC control sequence . Try send "\x1b\r"

+8
source

I tried to solve this issue in the last couple of days and finally found a solution :)

\x1b\r and \x1d\r did not work for me. I found a solution here: Using a 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 it works:

If your echo cannot directly control control characters, tr can do this for you. tr understands octal sequences. Create your output echo symbols that you don't need, and tr convert them to control characters you want.

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

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

tr reads four characters down the echo channel; it translates e to ESCape (octal 033), a to CTRL-a (octal 001) and w to CTRL-w (octal 027). The left bracket does not change; tr prints it as is.

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

+2
source

CTRL + R represents the escape sequence "^] to exit telnet

0
source

Source: https://habr.com/ru/post/956111/


All Articles