期待第29次迭代后循环死亡

时间:2015-07-01 21:08:10

标签: while-loop cygwin tcl expect

我在Cygwin中使用Expect脚本。它读取两个输入文件:一个是按完整主机名列出的网络设备,另一个是在记录输出时在这些设备上运行的命令列表。它一直有效,直到完成第29台设备。当spawn命令在第30台设备上执行时,我得到了这个输出:

send: spawn id exp65 not open
    while executing
"send -s "MyUserID\r""
    ("while" body line 30)
    invoked from within
"while {[gets $switches host] >= 0} {
        set hostname [string trimright $host]   ;# get rid of trailing whitespace
        if {[string length $hostname] == 0} {..."
    (file "./getna-lab.exp" line 37)

为了排除Cygwin的问题,我想用我通常使用Expect的Mac进行测试,但我不能再做了(设备是安全的,只能通过Windows虚拟桌面,因此Cygwin。)起初我以为这是因为来自telnet会话的“退出”不起作用而且它们仍处于打开状态,但事实并非如此;我尝试将“exit”添加到命令列表文件中,它肯定会执行。

下面列出了脚本和其他文件内容。感谢您的帮助。

#!/usr/bin/expect

set timeout 600

log_user 0
match_max 100000
set expect_out(buffer) {}

set switchlist [lindex $argv 0]
set commandlist [lindex $argv 1]

#
# Open switch list file
#
if [catch {open "$switchlist" "r"} switches] {
    puts stderr "$CR$switches$CE\n"
    exit 1
}

#
# Verify command list file opens OK before we logon, etc.
#
if [catch {open "$commandlist" "r"} commands] {
    puts stderr "$CR$commands$CE\n"
    exit 1
}
close $commands

#
# Enable trace logging
#
trace variable expect_out(buffer) w log_by_tracing

#
# loop for each device in switch list, looping for each command in command list
#
while {[gets $switches host] >= 0} {
    set hostname [string trimright $host]   ;# get rid of trailing whitespace
    if {[string length $hostname] == 0} {   ;# leave loop if blank line detected
        break
        }
    regexp {^([^\..]+)(\..*$)} $hostname domain hostshort ;# peel off domain name
    send_user "\n$hostname:\nWorking...\n"
    if [catch {open "$commandlist" "r"} commands] {
        puts stderr "$CR$commands$CE\n"
        exit 1
    }
    log_file -noappend -a "$hostshort.txt"

        spawn telnet $hostname
        expect \
            {
            "sername:" {}
            "ogin:" {}
            "assword:" {send -s "\r" 
                            expect \
                                {
                                "ogin:" {}
                                "sername:" {}
                                }
                }
            timeout {
                send_user "Timed out on device $hostshort logon.\n"
                break
            }
        }

        send -s "myUserID\r"
        expect {
            "assword:" {}
            timeout {
                send_user "Timed out on device $hostshort logon.\n"
                break
            }
        }

        send -s "MyPassword\r"
        expect {
            "$hostshort" {}
            timeout {
                send_user "Timed out on device $hostshort logon.\n"
                break
            }
        }

        while {[gets $commands command] >= 0} {
            send -s "$command\r"
            expect {
                "$hostshort#" {}
                timeout {send_user "Timed out waiting for $hostshort # prompt.\n"}
            } 
        }
    send -s "exit\r"
log_file
}

close $switches
close $commands

我通常使用如下所示的switchlist.txt文件:

switch1.domainname.com
switch2.domainname.com

我还使用一个名为commands.txt的文件来列出这样的命令:

show interface status
sh run

脚本通过

执行
# ./get-stuff.exp switchlist.txt commands.txt

1 个答案:

答案 0 :(得分:1)

我怀疑你可能因为没有在该循环中正确关闭生成的进程而耗尽(某些东西)。尝试用以下方法替换循环内容:

spawn telnet $hostname
expect {
    "sername:" { send -s "$myUserId\r"; exp_continue }
    "assword:" { send -s "MyPassword\r"; exp_continue }
    timeout {
        send_user "Timed out on device $hostshort logon.\n"
        continue   ;# next host
    }
    "$hostshort"
}

while {[gets $commands command] >= 0} {
    send -s "$command\r"
    expect {
        "$hostshort#" {}
        timeout {
            send_user "Timed out waiting for $hostshort # prompt.\n"
            continue   ;# next host
        }
    } 
}
send -s "exit\r"
expect eof
wait 
exp_close