2

I am unable to get Telnet to send only CR (\r) at the end of a command, despite setting the crlf toggle to <CR><NUL> (see example below). Instead, Telnet always sends CR-LF (\r\n).

My goal: retrieve data from a remote device connected to a cell-modem through an RS-232 port. Currently, I’m using Telnet to send my requests for the data. I have good reasons to believe this is possible, and I need to be able to scale up and automate.

Requirements: the remote device requires that commands end only in CR.

My understanding of the problem: I assume the extra LF is causing the device to fail to properly read my commands. More specifically, I think the LF is left over from each command, and added to the beginning of the next command. I infer this because the very first time I issue a command to the remote device after resetting the device, I actually get the correct response. All subsequent responses, however, indicate the device did not understand the command.

Details: if I launch Telnet, and set it to return hex values (set netdata, plus set prettydump to make it easier to read), I can see that the lines end in CR (hex=0d) and LF (hex=0a) even though asked Telnet to end the lines in only CR plus NULL. I.e., I expected the last character to be 00, not 0a. See snippet below (host and port replaced with "x"). FYI: "QD" is my command, which asks the device to report the current date, and "BADD" is the device's response when it doesn't understand a command.

$ telnet
telnet> toggle crlf
Will send carriage returns as telnet <CR><LF>.
telnet> toggle crlf
Will send carriage returns as telnet <CR><NUL>.
telnet> set netdata
Will print hexadecimal representation of network traffic.
telnet> set prettydump
Will print user readable output for "netdata".
telnet> open x.x.x.x xxxx
Trying x.x.x.x xxxx...
Connected to x.x.x.x.
Escape character is '^]'.
QD
> 0x0    51 44 0d 0a
< 0x0    42 41 44 44 0d 0a
BADD

Platforms I have tested: Ubuntu 18.04.1 using Telnet, inetutils-telnet, and PuTTY; Windows 7 using Cygwin, PuTTY, and TeraTerm; Windows 10 using PuTTY.

Similar post: Any way to send just "\n" in Telnet? – the question in this other post, however, was the reverse: the line endings needed to be LF only.

Summary of question: how do I get Telnet to send only CR at the ends of lines?

nuthatch
  • 21
  • 1
  • 4
  • Can you try either of the solutions in the other question? Either use netcat instead of telnet, or put telnet into binary mode and use either Enter or Control-M at the end of each line. – Mark Plotnick Jun 21 '19 at 01:47
  • What are you typing after ``QD``? – Scott - Слава Україні Jun 21 '19 at 02:27
  • @Mark: I've tried netcat, but don't get a response: `$ nc x.x.x.x xxxx`, Enter, then `QD`. Does the device need to be set to listening mode using netcat first? If so, that's not going to be possible. When I put Telnet into binary mode, the ending is LF only, regardless of whether I type Enter or Ctrl-M, which seems really weird, and I get no response from the device. The Ctrl-M idea was clever, so I tested that on my original example, but that made no difference: it ended the line with CRLF as before. @Scott: In my original example I was typing Enter after `QD`. – nuthatch Jun 21 '19 at 05:31
  • I have made progress using netcat. The following returns the date, as expected: `$ printf 'QD\r' | nc -w 5 x.x.x.x xxxx`. – nuthatch Jun 22 '19 at 04:55
  • 1
    +1 for using `netcat` rather than `telnet`. I bet most of this weirdness is coming from the `icrnl` terminal mode setting. Use `stty -a` to confirm that it's set. `icrnl` converts CRs into LFs, so your `telnet` and `netcat` are never actually seeing CR from the keyboard. You can `stty -icrnl` to let CR pass untranslated (and `stty icrnl` to revert) but then you'll need to use ctrl-J to indicate end-of-line. – ottomeister Jun 24 '19 at 16:16
  • @ottomeister: thank you for the suggestion and the detailed instructions. I tried it with different permutations (toggling crlf, binary/not binary), but despite the `-icrnl` setting in `stty`, Telnet still adds a LF onto the end. – nuthatch Jun 25 '19 at 04:50
  • I experimented more with `stty`. If I set `inlcr` (translate all LFs to CRs), Telnet can't do anything -- it just registers that it is getting CRs (either Ctrl-J, Ctrl-M, or Enter). Telnet seems to need LF to do anything, including sending a command to the server. Maybe `toggle crlf` was written knowing Telnet needs LF, and intended to drop that last LF before sending the command to the server, but doesn't seem to be actually doing that. I also experimented with `stty raw`, also with no luck. – nuthatch Jun 25 '19 at 17:30

0 Answers0