\ r来自哪里? [英] Where does the \r come from?

查看:53
本文介绍了\ r来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决许多需要登录的情况不能使用ssh密钥的服务器.结果我制定这是不良做法"的预期脚本:

I am trying to work around a situation where I need to login to a multitude of servers without being able to utilize ssh keys. As a result I am formulating a "that is a bad practice"-expect script:

#!/usr/bin/expect
set arg1 [lindex $argv 0]
spawn ssh $arg1 -l user "hostname; env x='() { :;}; echo vulnerable' bash -c \"echo this is a test\"; echo"
expect " password:"
send "my_supersecret_password\n"
interact

运行正常:

$ ./ssh.expect server
spawn ssh server -l user hostname; env x='() { :;}; echo vulnerable' bash -c "echo this is a test"; echo
user@server's password:
server
this is a test

$

但是在多个系统上运行时,我需要一个格式更好的列表,因此,我尝试让perl重新格式化数据:

But I need a better formatted list when running on more than one system, so I attempt to let perl reformat the data:

$ ./ssh.expect server | perl -e '$dump = <>; $dump = <>; chomp($line1 = <>); chomp($line2 = <>); $dump = <>; print "$line1:$line2\n";'
:this is a test

打印服务器名称,就好像它以\ r结尾.我不认为应该这样.你同意?打印服务器名称后,如何使系统不返回第0列?

The server name is printed as if it ends with a \r. I don't think that it should. Do you agree? How can I get the system to not return to column 0 after printing the server name?

我可以通过在打印中添加换行符来验证两个变量均包含数据:

I can verify that both variables contain data by adding a newline to my print:

$ ./ssh.expect server | perl -e '$dump = <>; $dump = <>; chomp($line1 = <>); chomp($line2 = <>); $dump = <>; print "$line1\n:$line2\n";'
server
:this is a test

如所评论,以下作品.

./ssh.expect server | tr -d '\r' | perl -e '$dump = <>; $dump = <>; chomp($line1 = <>); chomp($line2 = <>); $dump = <>; print "$line1:$line2\n";'
server:this is a test

不是要让tr多余吗?

推荐答案

Expect使用伪TTY与其生成的命令进行通信,在本例中为ssh进程.默认情况下,TTY和PTY将在输出文本中将\ n转换为\ r \ n(LF转换为CRLF).因此,除非您尝试删除CR,否则您期望脚本的输出将包含CRLF序列.您可以通过交互运行 expect 来看到此信息:

Expect uses a pseudo-TTY to communicate with the command that it spawns, which in this case is the ssh process. TTYs and PTYs by default will translate \n to \r\n (LF to CRLF) in output text. So the output of your expect script would contain CRLF sequences unless you took the effort to remove the CR's. You can see this by running expect interactively:

$ expect -c 'spawn echo foo; interact' | od -a
0000000    s   p   a   w   n  sp   e   c   h   o  sp   f   o   o  cr  nl
0000020    f   o   o  cr  nl                                            
0000025               ^^--^^--note

TTY LF-> CRLF转换由TTY"onlcr"标志控制.我尝试在Expect脚本中关闭该标志,但没有成功.

TTY LF->CRLF conversion is controlled by the TTY "onlcr" flag. I played around with turning that flag off in the expect script but wasn't successful.

Perl的 chomp 命令删除perl输入记录分隔符( $/变量)的序列,该序列在Unix系统上只是\ n.换句话说,默认情况下,\ r对Unix上的 chomp 并不特殊.如果愿意,您可以在perl脚本中更改 $/,以使chomp删除回车符.或者,您可以通过 tr 通过管道传输期望脚本的输出,如所讨论的那样.

Perl's chomp command removes sequences of the perl input record separator (the $/ variable), which will just be \n on Unix systems. In other words, \r isn't special to chomp on unix by default. You could alter $/ in your perl script to make chomp remove the carriage returns, if you like. Or you could pipe the output of your expect script through tr, as discussed.

这篇关于\ r来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆