如何抑制期望发送输出? [英] How to suppress expect send output?

查看:74
本文介绍了如何抑制期望发送输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Expect脚本,我希望它像一个精美的ssh程序一样运行,它会在运行命令之前跳入多台计算机并在目标计算机上设置环境.

I have an expect script which I'd like to behave as a fancy ssh program that hops several machines and sets up the environment on target machine before running the commands.

我可以使用log_user 0/1来关闭/打开来自期望的输出,这有助于密码提示和登录标语以及用于设置环境的命令.

I can use log_user 0/1 to turn off / on output from expect and that helps with password prompts and login banners, and commands to setup environment.

但是,就像ssh一样,一旦我的脚本开始发出命令,我就不想看到发出的命令.那就是我不想在发送命令\ n"后看到命令".我只想看一下命令的结果.

But, like ssh, once my script starts to issue commands, I don't want to see the issued command. That is I don't want to see "command" after send "command\n". All I want to see is the results of command.

如何抑制发送输出,而不抑制结果?

How do I suppress the send output, but not the results?

这是Expect脚本的一个片段:

Here's a snippet of the expect script:

log_user 1
foreach daline [lrange \$argv 0 end] {
   send "\$daline\r"
   set buffer1 
}

因此,在此循环之前,我发送密码和设置环境.然后在此循环中,我运行每个作为参数提供给期望值的bash命令.

So prior to this loop, I send password, and setup environment. Then in this loop, I run each bash command that was fed to the expect as an argument.

谢谢.

推荐答案

许多程序回显其输入.例如,如果将date命令发送到外壳,则会看到字符串date和日期.更准确地说,您将看到通常在终端上看到的所有内容.这也包括格式化.

Many programs echo their input. For example, if you send the date command to the shell, you will see the string date followed by a date. More precisely, you will see everything that you would ordinarily see at a terminal. This includes formatting, too.

send "date\r" 
expect -re $prompt 

上面的命令以expect_out (buffer)设置为date\r\nFri Nov 7 20:47:32 IST 2014\r\n结尾.更重要的是,已经回显了字符串日期.此外,每行以\r\n结尾,包括您发送的\r.日期的回显与send命令无关.

The command above ends with expect_out (buffer) set to date\r\nFri Nov 7 20:47:32 IST 2014\r\n. More importantly, the string date has been echoed. Also, each line ends with a \r\n, including the one you sent with a \r. The echoing of date has nothing to do with the send command.

换句话说,没有方法可以发送字符串,并且send不能回显它,因为send最初没有回显它.产生的过程是.

To put this another way, there is no way to send the string and have send not echo it because send is not echoing it in the first place. The spawned process is.

在许多情况下,产生的进程实际上将回显任务委托给终端驱动程序,但是结果是相同的-您将对流程的输入视为来自流程的输出.

In many cases, the spawned process actually delegates the task of echoing to the terminal driver, but the result is the same-you see your input to the process as output from the process.

通常,仅使用log_user(已在其他位置使用过)可以处理回显的输入.例如,假设已经建立了到远程主机的连接,并且您想获取远程日期,但是没有看到回显日期命令本身.一个常见的错误是写:

Often, echoed input can be handled by using log_user only (which you have used in different place). As an example, suppose a connection to a remote host has been spawned and you want to get the remote date, but without seeing the date command itself echoed. A common error is to write:

log_user 0 ;# WRONG 
send "date\r" ;# WRONG 
log_user 1 ;# WRONG 
expect -re .*\n ;# WRONG

运行时,log_user命令无效,因为expect直到expect命令才读取回显的日期".解决此问题的正确方法如下:

When run, the log_user command has no effect because expect does not read the echoed "date" until the expect command. The correct way to solve this problem is as follows:

send "date\r" 
log_user 0 
expect -re "\n(\[^\r]*)\r" ;# match actual date 

log_user 1 
puts "$expect_out(l,string)" ;# print actual date only 

如果要向远程外壳发送大量命令,则首先禁用所有回显可能更方便.您可以生成外壳程序,然后发送命令stty -echo,此后将不再回显您的命令. stty echo re启用回显.

If you are sending a lot of commands to a remote shell it may be more convenient to just disable all echoing in the first place. You can spawn a shell and then send the command stty -echo, after which your commands will no longer be echoed. stty echo re enables echoing.

spawn ssh <host>
stty -echo; # Disable 'echo' here
expect something

#Your further code here

stty echo # Enable 'echo' here

#Close of connection

参考资料:探索期望

这篇关于如何抑制期望发送输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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