telnet输出在哪里?
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl
spawn telnet rainmaker.wunderground.com
getting weather for nyc
^C
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$
main:
lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/api
package require weather 1.0
tutstack::connect "nyc"
代码:
package provide weather 1.0
package require Tcl 8.5
package require Expect
namespace eval ::tutstack {
}
proc ::tutstack::parse {city} {
puts "getting weather for $city"
expect -nocase "Press Return to continue:"
#interact 04 return
interact 04 return
}
proc ::tutstack::connect {city} {
spawn telnet rainmaker.wunderground.com
set telnet $spawn_id
#interact
parse $city
}
这项工作:
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh chainedProcs.tcl
hello Alice from first
hello Alice from second
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat chainedProcs.tcl
lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/chained
package require chained 1.0
example::first "Alice"
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat chained/chained.tcl
package provide chained 1.0
namespace eval ::example {
}
proc ::example::first {foo} {
puts "hello $foo from first"
second $foo
}
proc ::example::second {bar} {
puts "hello $bar from second"
}
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$
但是。。。没有在那里使用telnet。我正在寻找"链"(?(一个序列,但与telnet,期望,交互,等等
无论何时在过程中使用Expect包的命令,都需要格外小心,因为它访问变量的方式。特别是,你可能至少需要说:
global spawn_id
在每一个程序中。也许是这样的:
proc ::tutstack::parse {city} {
global spawn_id
puts "getting weather for $city"
expect -nocase "Press Return to continue:"
# You *might* need inter_return instead of return; the documentation isn't clear
interact " 04" return
}
proc ::tutstack::connect {city} {
global spawn_id
spawn telnet rainmaker.wunderground.com
set telnet $spawn_id
parse $city
}
然而,您最好将派生ID(即调用spawn
的结果(保留在命名空间变量中,并通过-i
标志将其显式传递到相关命令中,如下所示:
proc ::tutstack::connect {city} {
variable telnet [spawn telnet rainmaker.wunderground.com]
parse $city
}
proc ::tutstack::parse {city} {
variable telnet
puts "getting weather for $city"
expect -i $telnet -nocase "Press Return to continue:"
# You *might* need inter_return instead of return; the documentation isn't clear
interact -i $telnet " 04" return
}