35

I am using following script to reboot my router using Telnet:

#!/usr/bin/env python

import os
import telnetlib
from time import sleep

host = "192.168.1.1"
user = "USER"
password = "PASSWORD"
cmd = "system restart"

tn = telnetlib.Telnet(host)
sleep(1)

tn.read_until("Login: ")
tn.write(user + "\n\r")
sleep(1)

tn.read_until("Password: ")
tn.write(password + "\n\r")
sleep(1)

tn.write(cmd + "\n\r")

I don't know why but removing "\r" breaks this code. So what does "\r" do in this script and when do I use "\r" in general?

Note: I know about "Carriage Return" but still could not figure out it is used in my script. I am running this script in Linux.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Alinwndrld
  • 777
  • 2
  • 6
  • 13
  • 2
    It's a [Carriage return](https://en.wikipedia.org/wiki/Carriage_return) – sloth Jan 30 '13 at 14:53
  • I used google to read everything about carriage return but still could not figure out its use in my above script. – Alinwndrld Jan 30 '13 at 14:55
  • This question may have been asked also because of what printing just an \r character does not do, which is advance the line. – octopusgrabbus Nov 20 '13 at 17:50
  • i suggest you also take look at : http://docs.python.org/release/2.2.3/ref/strings.html which explains all escape characters – Hugo Jan 30 '13 at 15:04

4 Answers4

42

The '\r' character is the carriage return, and the carriage return-newline pair is both needed for newline in a network virtual terminal session.


From the old telnet specification (RFC 854) (page 11):

The sequence "CR LF", as defined, will cause the NVT to be positioned at the left margin of the next print line (as would, for example, the sequence "LF CR").

However, from the latest specification (RFC5198) (page 13):

  1. ...

  2. In Net-ASCII, CR MUST NOT appear except when immediately followed by either NUL or LF, with the latter (CR LF) designating the "new line" function. Today and as specified above, CR should generally appear only when followed by LF. Because page layout is better done in other ways, because NUL has a special interpretation in some programming languages, and to avoid other types of confusion, CR NUL should preferably be avoided as specified above.

  3. LF CR SHOULD NOT appear except as a side-effect of multiple CR LF sequences (e.g., CR LF CR LF).

So newline in Telnet should always be '\r\n' but most implementations have either not been updated, or keeps the old '\n\r' for backwards compatibility.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
24

'\r' means 'carriage return' and it is similar to '\n' which means 'line break' or more commonly 'new line'

in the old days of typewriters, you would have to move the carriage that writes back to the start of the line, and move the line down in order to write onto the next line.

in the modern computer era we still have this functionality for multiple reasons. but mostly we use only '\n' and automatically assume that we want to start writing from the start of the line, since it would not make much sense otherwise.

however, there are some times when we want to use JUST the '\r' and that would be if i want to write something to an output, and the instead of going down to a new line and writing something else, i want to write something over what i already wrote, this is how many programs in linux or in windows command line are able to have 'progress' information that changes on the same line.

nowadays most systems use only the '\n' to denote a newline. but some systems use both together.

you can see examples of this given in some of the other answers, but the most common are:

  • windows ends lines with '\r\n'
  • mac ends lines with '\r'
  • unix/linux use '\n'

and some other programs also have specific uses for them.

for more information about the history of these characters

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • 3
    sorry, but don't agree. It is the TELNET protocol itself which requires `\r\n` (defined as the end-of-line sequence in the protocol). See [RFC854](https://tools.ietf.org/html/rfc854). – isedev Jan 30 '13 at 14:57
  • @isedev i am not sure how your comment relates to my answer. i think you commented to the wrong answer, or please explain yourself further. – Inbar Rose Jan 30 '13 at 15:03
  • Still, I say it's \r\n that is [defined](http://www.freesoft.org/CIE/RFC/1123/31.htm), despite the fact that \n\r also works. – class stacker Jan 30 '13 at 15:04
  • @Inbar: just mean it has nothing to do with operating system in this case. It is defined by TELNET protocol spec, independently of OS. – isedev Jan 30 '13 at 15:05
  • @isedev did i say it did? you are giving criticism to a point that i did not even make? – Inbar Rose Jan 30 '13 at 15:06
  • @inbar: not as such, but I think your answer was misleading, mentioning differences between OS, when OS has nothing to do with it. Is that really so offensive? – isedev Jan 30 '13 at 15:11
  • @isedev what i wrote is 'you can see examples of this given in some of the other answers, but the most common are' im just summarizing the answers given by other users so as to make a cohesive answer. – Inbar Rose Jan 30 '13 at 15:25
  • 2
    @InbarRose A little of topic but regarding your comment \r is similar to \n. I think they are different on (atleast linux I guess). check out the example : `print "bbb\raaaa" # output : aaaa` `print "__bbb\raaaa" # output : aaaab` `print "_____bbb\raaaa" # output : aaaa_bbb` Here \r is signals end of line and the cursor moved to the start of line and subsequent characters are printed from there. – pravin Jan 19 '15 at 16:06
7

\r is the ASCII Carriage Return (CR) character.

There are different newline conventions used by different operating systems. The most common ones are:

  • CR+LF (\r\n);
  • LF (\n);
  • CR (\r).

The \n\r (LF+CR) looks unconventional.

edit: My reading of the Telnet RFC suggests that:

  1. CR+LF is the standard newline sequence used by the telnet protocol.
  2. LF+CR is an acceptable substitute:

The sequence "CR LF", as defined, will cause the NVT to be positioned at the left margin of the next print line (as would, for example, the sequence "LF CR").

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    sorry, but don't agree. It is the TELNET protocol itself which requires `\r\n` (defined as the end-of-line sequence in the protocol). See [RFC854](https://tools.ietf.org/html/rfc854). – isedev Jan 30 '13 at 14:56
  • @isedev: I am seeing lots of mentions of `CR LF` in the RFC, and only one mention of `LF CR`. Could you point to a specific page/section? – NPE Jan 30 '13 at 15:02
  • Being dislexic, did mean the usual `\r\n`. Corrected comments and answer. My point was really it has nothing to do with OS, but TELNET protocol itself (i.e. required for TELNET on Linux and TELNET on Windows). – isedev Jan 30 '13 at 15:02
0

Actually, this has nothing to do with the usual Windows / Unix \r\n vs \n issue. The TELNET procotol itself defines \r\n as the end-of-line sequence, independently of the operating system. See RFC854.

Community
  • 1
  • 1
isedev
  • 18,848
  • 3
  • 60
  • 59
  • 2
    Umm, no; TELNET defines CR LF aka \r\n as the eond-of-line sequence. – class stacker Jan 30 '13 at 15:00
  • 2
    Everybody started teaching me about carriage return which i already know and started downvoting my question without answering. I think you are correct about Telnet. – Alinwndrld Jan 30 '13 at 15:01