Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "\r" do in the following script?

Tags:

python

telnet

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.

like image 993
Alinwndrld Avatar asked Jan 30 '13 14:01

Alinwndrld


People also ask

What does \r do in text?

The /r stands for return or carriage return which owes it's history to the typewriter. A carriage return moved your carriage all the way to the right so you were typing at the start of the line. The /n stands for new line, again, from typewriter days you moved down to a new line.

What does \r do in JavaScript?

The RegExp \r Metacharacter in JavaScript is used to find the carriage return character (Carriage return means to return to the beginning of the current line without advancing downward). If it is found it returns the position else it returns -1.

What does print \r do?

It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character. This is called "escaping".

What is the use of \r in Python?

A carriage return is nothing but a simple escape character. \n is also an escape character which creates a new line. Carriage return or \r is a very unique feature of Python. \r will just work as you have shifted your cursor to the beginning of the string or line.


2 Answers

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.

like image 113
Some programmer dude Avatar answered Sep 30 '22 23:09

Some programmer dude


'\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

like image 30
Inbar Rose Avatar answered Sep 30 '22 22:09

Inbar Rose