Why does paramiko returns \r\n as newline instead of \n?

Viewed 4173

I'm using a python SSH library named "paramiko". It uses like this:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user_name, password=password)
chan = ssh_pool[host].get_transport().open_session()
chan.exec_command(SOME_COMMAND)

Because I need to execute some command in sudo, I need to call chan.get_pty() before exec_command to ensure there is a tty avaliable.

And I found after I called get_pty(), the stdout return from remote server using \r\n as newline instead of \n.

For example:

>>> chan = ssh.get_transport().open_session()
>>> chan.exec_command("echo hello world")
>>> chan.recv(1000)
b'hello world\n'

and after calling get_pty():

>>> chan = ssh.get_transport().open_session()
>>> chan.get_pty()
>>> chan.exec_command("echo hello world")
>>> chan.recv(1000)
b'hello world\r\n'

I have do some web searching, but cannot find anything connect to pty / tty / newline / \r\n

I'm worried about what may cause this change.

Why does paramiko returns \r\n as newline instead of \n?

1 Answers
Related