期望推荐写入文件



我正在编写一个期望脚本,该脚本将ssh进入多个IP,以测试其连接性。我想包括一个" puts"语句,将每个测试的结果写入调用脚本的机器上的文件。尽管我认为我正在遵循该手册以写入文件的手册,但它只写了to to stdout。请参阅下面的脚本。最终结果是该文件是在本地计算机上创建的,但是没有输出将其引向其。相反,输出转到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 "$PASSWORDn"}
                expect {
                        -re ".*Are you sure you want to continue connecting.*" {send "yesn"; exp_continue}
                        -re ".*$PROMPT.*$.*"  {puts $chan "$IPSTART$IPENDn"; send "exitn"}
                }
        close $chan
"
done

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

供参考,这是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

您可能的问题是您正在使用双重报价。因此,您应该逃脱将传递给期望的文字$(即$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 "$PASSWORDn"}
                expect {
                        -re ".*Are you sure you want to continue connecting.*" {send "yesn"; exp_continue}
                        -re ".*$PROMPT.*$.*"  {puts $chan "$IPSTART$IPENDn"; send "exitn"}
                }
        close $chan
"
done

好吧,我不知道如何在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 "$PASSWORDn"}
                        timeout {puts $CHAN "$IPSTART$IPEND does not work"}
                }
                expect {
                        -re ".*Are you sure you want to continue connecting.*" {send "yesn"; exp_continue}
                        -re ".*$PROMPT.*$.*"  {puts $CHAN "$IPSTART$IPEND works"; send "exitn"}
                }
}
close $CHAN

相关内容

  • 没有找到相关文章

最新更新