• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Need to echo a "^]" in script

I'm trying to write a script that telnets to a remote DIGI printer port, sends the print job and then disconnect from the remote port by sending a "^]" ( "ctrl-]" which is the telnet escape character). It works perfectly as far as establishing the telnet connection and sending the print job to the printer, but I cannot figure out how to send a "^]" by using the ECHO command. Any one know how this could be done, or is there a better command I could use in the script to break the telnet connection?
1 Solution
The quick answer is while using vi to edit the script, instead of typing "^]", type the following keystrokes:
ctrl+v esc ]

In vi, as the escape character is used to switch from edit mode to command mode, you need to use ctrl+v which "quotes" special characters in edit mode.

Further Thoughts
Using telnet is not a great solution. Consider having a look at something like netcat.

nascar_3Author Commented:

 Thanks for the quick answer, but that didn't really work as such. I mean, I'm familiar with the <Ctrl-V> in vi, in fact I've used it in this same script for a CR by doing <Ctrl-V> then <ENTER>. It shows up in the script as ^M and issues a carriage return, no problem. BUT when I tried <Ctrl-V> then <Ctrl-]> the result isn't the same. I DID TRY the <Ctrl-V> then <ESC-]> like you suggested, but it shows up as ^[] in the script and just prints a "]" when it runs.

 I know Telnet isn't ideal for this, but this is a short term application and doesn't warrant exploring new avenues like netcat (not sure what that is even). I'm stuck with what I have. My only problem is that I cannot get the telnet session to break after its connected.

It will break just fine when I try it from a CRT by actually hitting <Ctrl-]>+<ENTER>. But I need it to break automatically by echoing that sequence directly after a print job finishes.

Again, maybe the keystrokes needed after the <Ctrl-V> are just somewhat different than what I've tried, or there is a better command than "echo" to do the job ?
When you tried <Ctrl-V> then <Ctrl-]> what was the result?
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Oops, re-reading my answer makes me realise I may have been a little tired when I wrote it ;-).
Try this:
ctrl+v ctrl+]

It should come out as "^]" in vi and appear as 1 character. If this doesn't work, you may need to have a look at something like expect to solve the problem.
Another trick you can use is wrap the program in a shell-script. Assume the program you're running is called "foo". A shell-script called "bar" can be created that runs foo, waits for a few seconds and then issues a kill to foo. Again, messy.

nascar_3Author Commented:

 Thanks, but If you re-read my first rejection, I had already tried the <ctrl+v> <ctrl+]> and did not get the desired result.

to answer OZO's question, this does just what grantk said, it shows as a ^] in the script as 1 character, but is NOT breaking the telnet connection. The line reads:
echo "^]^M" |lp (output to serial port hooked to DIGI)

again, if I hook up a CRT and hit the actual keys <Ctrl+]> and then <ENTER> the connection breaks just fine...

Just to clarify what I'm trying to do....

DG UNIX system (NO network TCP/IP on this system)
DG serial printer output to DIGIPORT-A[port#3]
DG sends echo command to TELNET DIGI PORTSERVERS together
printjob is sent to printer hooked to PORTSERVER-B[port#8]
printjob prints great! (everything great to this point)
This is where I need to "^]" TELNET to get a command line
echo command "close" TELNET session and free up ports
Next print job would start TELNET over again...........

I need to cancel the TELNET to allow print jobs from a different AIX system to also go to that same printer on PORTSERVER-B[port#8] coming from the IP network. Those print jobs won't go to printer as long as TELNET session from DG is active. Once TELNET closes and ports are free, the AIX print jobs are allowed to continue on to printer.  
There are several options:
1) If you use bash then "echo -e '\E'" will output <ESC> character.
2) If you cannot make echo print <ESC>, then make telnet understand some character you can print as an escape sequence. Use "telnet -e ^X" to set ^X as an escape character (or use your favorite)
3) You may just kill telnet without all this escape tricks.
Result will be the same and it's way easier and more reliable. After all, you can always use "kill -9". :-)
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.

Please click on your Member Profile, select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.
Moderator @ Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now