110*_*2ch 5 linux ssh io-redirection cat
我用 putty 从 windowsbox 连接到 linux box。之后我执行以下操作:
在服务器A上:
serverA: file /etc/motd
/etc/motd: : ASCII English text
Run Code Online (Sandbox Code Playgroud)
在服务器B上:
serverB: ssh -t user@serverA "cat /etc/motd" > /etc/motd.serverA
serverB: file /etc/motd.serverA
/etc/motd.serverA: ASCII text, with CRLF line terminators
Run Code Online (Sandbox Code Playgroud)
为什么重定向后的输出现在有 CR 和 LF?仅当使用 ssh 的 -t 选项时才会发生这种情况。如果我需要使用 sudo 在 ssh 登录上运行命令,则需要 -t 。例如:
serverB: ssh -t user@serverA "sudo cat /etc/shadow" > /etc/shadow
Run Code Online (Sandbox Code Playgroud)
感谢您的建议
检查 TTY 设置。
$ ssh -t somewhere 'stty -a' | grep cr
iflags: -istrip icrnl -inlcr -igncr -iuclc ixon -ixoff ixany imaxbel
oflags: opost onlcr -ocrnl -onocr -onlret -olcuc oxtabs -onoeot
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -mdmbuf
Run Code Online (Sandbox Code Playgroud)
这些可能会有所不同,但它们在这里显示,默认情况下,ssh -t“igncr忽略 CR”在输入上被禁用,并且对于已onlcr设置的输出(将 NL 映射到 CR-NL),并且 CR 不会被破坏或省略。人们可以在手册中查找这些术语stty(1),也可以查看termios(4)(Linux 可能会将其放在其他一些 man 部分中)。
这些设置也可以修改(但这可能会破坏出于某种原因需要onlcr设置的东西):
$ ssh -t somehost 'stty onlcr; cat /etc/motd' > x ; file x
x: ASCII English text, with CRLF line terminators
$ ssh -t somehost 'stty -onlcr; cat /etc/motd' > x ; file x
x: ASCII English text
$
Run Code Online (Sandbox Code Playgroud)
更明智的做法是使用scp或sftp复制数据来消除(伪)tty CR/NL 处理导致文件内容更改的风险。