当我想通过使用epext将我的操作打包到函数中时出现问题,以下是代码的样子,命令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}
#}
}
答案 0 :(得分:2)
您可以尝试使用spawn_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