Tcl 如何使用expect将我的操作打包到函数中

Tcl 如何使用expect将我的操作打包到函数中,tcl,expect,Tcl,Expect,当我想使用eText将操作打包到函数中时,我遇到了一个问题,下面是代码的样子,当命令ifconfig打包到函数中时将不会执行,我哪里做错了,如何将操作打包到函数中?提前谢谢 spawn ssh x.x.x.x proc do_sth {} { send "ifconfig\r" # won't work expect "~\]\#" {exit 0} } expect { "*assword" { send "xxx\r"; exp_continue } "~\]\#" {

当我想使用eText将操作打包到函数中时,我遇到了一个问题,下面是代码的样子,当命令ifconfig打包到函数中时将不会执行,我哪里做错了,如何将操作打包到函数中?提前谢谢

spawn ssh x.x.x.x

proc do_sth {} {
  send "ifconfig\r" # won't work
  expect "~\]\#" {exit 0}
}

expect {
  "*assword" { send "xxx\r"; exp_continue }
  "~\]\#" { do_sth }
  #"~\]\#" { 
  #  send "ifconfig\r"  # this would works fine
  #  expect "~\]\#" {exit 0}
  #}
}

您可以尝试使用
spawn\u id

spawn ssh x.x.x.x
#After process creation the process id will be saved in 
#standard expect variable'spawn_id'
#Copying it to variable 'id'
set id $spawn_id
现在,变量“id”保存对ssh进程的引用。我们可以很好地将send和expect与spawn id一起使用

#Now we are setting the spawn id to our ssh process to make sure 
#we are sending the commands to right process
#You can pass this variable 'id' as arg in 'do_sth'   
proc do_sth { id } {
    set spawn_id $id
    send "ifconfig\r"
    expect "~\]\#" {exit 0}
}
或者另一种方式是:

 proc do_sth { id } {
    #This way is useful, when u want to send and expect to multiple process 
    #simultaneously.
    send -i $id "ifconfig\r"
    expect -i $id "~\]\#" {exit 0}
}
像往常一样,您可以简单地按如下方式调用它们

do_sth $id