我有一个试图从 whatismyip 站点获取主页的期望脚本。我需要同时捕获 - 站点的 IP 和 HTTP 返回码:
#!/usr/bin/expect -f
set timeout -1
spawn telnet www.whatismyip.com 80
expect "Connected to www.whatismyip.com*"
set output $expect_out(0,string)
regexp {Connected to www\.whatismyip\.com.*?(\d+\.\d+\.\d+\.\d+)} $output match ip
send -- "GET / HTTP/1.0\n"
send -- "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4\n"
send -- "Host: www.whatismyip.com\n"
send -- "\n"
send -- "\n"
set output $expect_out(buffer)
regexp {.*HTTP/1.1 200 OK.*} $output match ret
puts $ip
puts $ret
expect eof
exit 0
有两个问题。首先,我将 IP 截断为最后一个字符,并得到未找到变量 ret 的错误:
spawn telnet www.whatismyip.com 80
Trying 108.162.200.37...
Connected to www.whatismyip.com (108.162.200.37).
Escape character is '^]'.
108.162.200.3
can't read "ret": no such variable
while executing
"puts $ret"
(file "./t2" line 15)
我尝试了所有方法和可能性,但无法同时纠正它们。请让我知道如何纠正这个问题。