如何使用Tcl从proc产生telnet? [英] how to spawn telnet from a proc with Tcl?

查看:165
本文介绍了如何使用Tcl从proc产生telnet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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$ 

主要:

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 \004 return
interact \004 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,期望,交互等.

but...not using telnet there. I'm looking to "chain" (?) a sequence but with telnet, expect, interact, etc.

推荐答案

每当在过程中使用Expect程序包的命令时,由于它访问变量的方式都需要格外小心.特别是,您可能至少需要说:

Whenever you are using the Expect package's commands in a procedure, you need to take some care because of the way it accesses variables. In particular, you probably need to say at least:

global spawn_id

在每个过程中

.也许像这样:

in each of those procedures. Perhaps like this:

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 "\004" return
}

proc ::tutstack::connect {city} {
    global spawn_id
    spawn telnet rainmaker.wunderground.com
    set telnet $spawn_id
    parse $city
}

但是,最好将生成ID(即调用spawn的结果)保留在名称空间变量中,并通过-i标志将其显式传递给相关命令,如下所示:

However, you're probably better off keeping the spawn ID (i.e., the result of calling spawn) in a namespace variable and passing it explicitly into the relevant commands via the -i flag, like this:

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 "\004" return
}

这篇关于如何使用Tcl从proc产生telnet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆