bash 期望将写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13052232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:52:14  来源:igfitidea点击:

expect puts write to file

filebashexpectchannelputs

提问by Rusty Lemur

I am writing an expect script that will ssh into several IPs to test their connectivity. I would like to include a "puts" statement to write the result of each test to a file on the machine calling the script. Although I think I'm following the manual for puts to write to a file, it only writes to stdout. Please see the script below. The end result is that the file is created on the local machine, but no output is directed to it. Instead, the output goes to stdout.

我正在编写一个 expect 脚本,它将通过 ssh 连接到多个 IP 以测试它们的连接性。我想包含一个“puts”语句,将每个测试的结果写入调用脚本的机器上的文件中。虽然我认为我正在遵循 puts 写入文件的手册,但它只写入 stdout。请参阅下面的脚本。最终结果是文件是在本地机器上创建的,但没有输出定向到它。相反,输出到标准输出。

#!/bin/bash

USER=user
PASSWORD=password
IPSTART=12.34.56.
OUTFILE="TEST.log"
PROMPT="#"

for IPEND in `seq 200 231`
do

expect -c "
        set timeout 3
        set chan [open $OUTFILE w]
        spawn ssh $USER@$IPSTART$IPEND
                expect -re \".*ssword.*\" {send \"$PASSWORD\n\"}

                expect {
                        -re \".*Are you sure you want to continue connecting.*\" {send \"yes\n\"; exp_continue}
                        -re \".*$PROMPT.*$.*\"  {puts $chan \"$IPSTART$IPEND\n\"; send \"exit\n\"}
                }

        close $chan
"

done

I'm wondering if there might be an issue with the quoting, but I can't figure it out.

我想知道引用是否有问题,但我无法弄清楚。

For reference, this is the example from http://www.tcl.tk/man/tcl8.4/TclCmd/puts.htm

作为参考,这是来自http://www.tcl.tk/man/tcl8.4/TclCmd/puts.htm的示例

set chan [open my.log a]
set timestamp [clock format [clock seconds]]
puts $chan "$timestamp - Hello, World!"
close $chan

回答by Janito Vaqueiro Ferreira Filho

You're probable problem was that you were using double-quotes. Therefore you should escape the literal $that will be passed to expect (ie. \$chan):

你可能的问题是你使用了双引号。因此,您应该转义$将传递给 expect (即。\$chan)的文字:

#!/bin/bash

USER=user
PASSWORD=password
IPSTART=12.34.56.
OUTFILE="TEST.log"
PROMPT="#"

for IPEND in `seq 200 231`
do

expect -c "
        set timeout 3
        set chan [open $OUTFILE w]
        spawn ssh $USER@$IPSTART$IPEND
                expect -re \".*ssword.*\" {send \"$PASSWORD\n\"}

                expect {
                        -re \".*Are you sure you want to continue connecting.*\" {send \"yes\n\"; exp_continue}
                        -re \".*$PROMPT.*$.*\"  {puts $chan \"$IPSTART$IPEND\n\"; send \"exit\n\"}
                }

        close $chan
"

done

回答by Rusty Lemur

Well, I coulnd't figure out how to do it in bash, so I changed the whole script to expect. It works now.

好吧,我不知道如何在 bash 中做到这一点,所以我将整个脚本更改为预期。它现在有效。

#!/bin/expect

set USER "user"
set PASSWORD "password"
set IPSTART "12.34.56."
set OUTFILE "TEST.log"
set PROMPT "#"
set CHAN [open $OUTFILE w]

for {set IPEND 200} {$IPEND <= 231} {incr IPEND} {
        spawn ssh $USER@$IPSTART$IPEND
                set timeout 3
                expect {
                        -re ".*ssword.*" {send "$PASSWORD\n"}
                        timeout {puts $CHAN "$IPSTART$IPEND does not work"}
                }

                expect {
                        -re ".*Are you sure you want to continue connecting.*" {send "yes\n"; exp_continue}
                        -re ".*$PROMPT.*$.*"  {puts $CHAN "$IPSTART$IPEND works"; send "exit\n"}
                }
}

close $CHAN