为什么 paramiko returns \r\n 作为换行符而不是 \n?

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

我正在使用名为 "paramiko" 的 python SSH 库。 它是这样使用的:

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)

因为我需要在sudo中执行一些命令,我​​需要在chan.get_pty()之前调用chan.get_pty() =]exec_command 以确保有可用的 tty。

我发现在调用 get_pty() 后,来自远程服务器的标准输出 return 使用 \r\n 作为换行符而不是 \n.

例如:

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

调用 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'

我做了一些网络搜索,但找不到任何连接到 pty / tty / newline / \r\n

的内容

我担心是什么导致了这种变化。

为什么 paramiko returns \r\n 作为换行符而不是 \n?

因为这是为(伪)终端翻译换行符的方式。 \r 是将光标移动到第 1 列的回车 return,\n 是将光标向下移动一位的换行符。

终端默认启用 onlcr 翻译标志。来自 stty 手册页:

[-]onlcr

newline performs a carriage return

您还可以看到此标志已启用,即在 stty -a.

的输出中前面(倒数第三行)没有 -
% stty -a
speed 38400 baud; rows 46; columns 79; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

您可以使用 stty -onlcr 禁用此设置,但它的存在通常不会成为问题;如果您怀疑某些二进制数据可能已损坏,无论如何都不能使用 pty。


如果这只是一些单一的命令,那么通过 SSH 发送明文密码并不会增加多少安全性;如果现在有人可以访问您的远程帐户,他们不仅可以无限制地访问该计算机上的 root,而且他们还会知道您的密码。让ssh root 更好,让root 运行 这1条命令用"forced commands", or edit /etc/sudoers so that no password is required for this single command from this account.