分享一个检查telnet瞬断的脚本,我用调用它来检测thrift的TCP9090端口一连上就断开连接的状态。

直接上代码了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/local/bin/expect
spawn telnet 127.0.0.1 9090
set timeout  1
expect {
        "*Escape character is*" {
                exp_continue
        }
        "*Connection closed by foreign host*" {
                send "note: 1) detect a unsafe connection"
        }
}
expect {
        "*Connection closed by foreign host*" {
                send "note: 2) detect a unsafe connection"
        }
        send "\\003"
        exit
}
expect "*Connection closed by foreign host*"
send "note: 3) disconnect by client cause timeout or not immediately exit expect"
exit
expect eof

exit
expect eof

调用者程序可以根据expect输出的note: 3) disconnect by client cause timeout or not immediately exit expect得知telnet上去瞬间失去连接。

发表评论