期望脚本在第56次迭代时完全停止

时间:2016-01-08 05:49:12

标签: unix tcl expect

我编写了一个expect脚本来检测我的盒子突然停止响应ssh / telnet请求的情况。我已经看到了关于关闭生成的会话和执行等待操作的其他答案。但我没有让我等待工作。请帮忙。

问题1 。在测试时,我看到我的脚本在第56次迭代时完全停止。无法找到它。

它显示的错误如下:

spawn telnet 10.16.206.15
too many programs spawned?  could not create pipe: too many open files
    while executing
"spawn telnet $ip_addr"
    ("while" body line 8)
    invoked from within
"while (1) {
                        match_max 100000
                        set hostname "Dell"
                        set user "admin"
                        set passwd "admin"
                        set timeout 20
                        set ip_addr "10.16.206.15"
                        sp..."
    (file "./trigger_on_cond.sh" line 3)

问题2 。另外,我看到“等待”(评论)实际上阻止了脚本。据我所知,它具有与UNIX wait()调用相同的功能。

脚本失败

#!/usr/bin/expect -f
    set count 0
    while (1) {
            match_max 100000
            set hostname "MyBox"
            set user "admin"
            set passwd "admin"
            set timeout 50
            set ip_addr "xx.yy.zz.15"
            spawn telnet $ip_addr

            expect {
                    "Login:" {
                        send "$user\r"
                        expect "Password"
                        send "$passwd\r"
                    }
                    "Unable to connect to remote host" {

                        exec date > /f10/topout.txt
                        sleep 2
                        exec top -b -o res all > /mypath/topout.txt
                        exec f10livecore sysd
                        sleep 20
                        exec top -b -o res all >> /mypath/topout.txt
                        sleep 10
                        exec fstat >>  /mypath/topout.txt
                        sleep 20
                        exec ps -laux >>  /mypath/topout.txt
                        exit
                    }
                    timeout {
                        send \003
                        exec date > /mypath/topout.txt
                        sleep 2
                        exec top -b -o res all > /mypath/topout.txt
                        exec f10livecore sysd
                        sleep 20
                        exec top -b -o res all >> /mypath/topout.txt
                        sleep 10
                        exec fstat >>  /mypath/topout.txt
                        sleep 20
                        exec ps -laux >>  /mypath/topout.txt
                        sleep 30
                        exit
                    }
            }

            expect {
                "$hostname>" {
                        #send "quit\r"
                        #expect eof
                        send "show ver\r"
                        set count [expr $count + 1]
                        puts $count
                        send "quit\r"
                        expect eof

                }
                "$hostname#" {
                        send "show ver\r"
                        expect "$hostname#"
                        #send "quit\r"
                        #expect eof
                        send "show ver\r"
                        set count [expr $count + 1]
                        puts $count
                        send "quit\r"
                        expect eof

                }
                timeout {
                        send \003
                        exec date > /mypath/topout.txt
                        sleep 2
                        exec top -b -o res all > /mypath/topout.txt
                        exec killcore sysd
                        sleep 20
                        exec top -b -o res all >> /mypath/topout.txt
                        exit
                    }

            }

            #wait
            sleep 30
        }

1 个答案:

答案 0 :(得分:0)

生成的telnet进程永远不会关闭,因此您达到了“max open files”的限制。 (linux:ulimit -n

只需尝试将expect eof替换为wait,等待telnet进程完成。

一种不优雅的方式是:

    ...
    set pid [exp_pid -i $spawn_id]
    exec kill -9 $pid
    wait
    ...