我有这样的输出:
Node Name Status IP Address
======================================================
bw2acn0 ENABLED x.x.x.x
bw2acn1 ENABLED x.x.x.x
bw2dvn0 ENABLED x.x.x.x
bw2dvn1 ENABLED x.x.x.x
bw2vm0 ENABLED x.x.x.x
bw2vm1 ENABLED x.x.x.x
我想创建一个循环来查看此输出是否包含任何应用程序的名称。
#!/opt/tools/unsupported/expect-5.39/bin/expect
set HOST [ lindex $argv 0 ]
set USER [ lindex $argv 1 ]
set PASSWORD [ lindex $argv 2 ]
set APP1 [ lindex $argv 3 ]
set APP2 [ lindex $argv 4 ]
set APP3 [ lindex $argv 5 ]
set APP4 [ lindex $argv 6 ]
spawn ssh -l $USER $HOST
expect_after eof {exit 0}
set timeout 120
expect "password:" { send "$PASSWORD\r" }
expect "~]#" { set buff $expect_out(buffer)
foreach i $APPS {
if {[regexp {"${i}"} $buff]} {
log_file ../report.txti
send_log "Commit nodes on $i ------------------------------- Passed\n\n"
puts "*********************paased"
} else {
log_file ../report.txt
send_log "Commit nodes on $i ------------------------------ Failed\n\n"
puts "******************failed"
}
}
}
log_file
send "\r"
expect "~]#" { send "date\r" }
expect "~]#" { send "exit\r" }
但我得到的只是它失败了,尽管它应该通过。
答案1
if { $buff match {*$APP$i*} } {
什么是match
?有expr
文档中没有任何内容使用该术语的。还有APP
变量是从哪里来的?你有APP1
等等,但没有APP
。
这string
指挥组可以将字符串与string match
, 和 an匹配array
(其他哪些语言称为哈希或关联数组)可能更好地对应用程序(节点?)名称进行分组,而不是尝试使用变量作为变量名称:
set theapps(app1) foo
set theapps(app2) bar
set theapps(app3) zot
set buff "blah bar blah"
foreach {name value} [array get theapps] {
if {[string match "*$value*" $buff]} {
puts "ok - $name $value"
} else {
puts "not ok - $name $value"
}
}
运行时,它bar
匹配app2
:
$ tclsh choices
not ok - app3 zot
not ok - app1 foo
ok - app2 bar
$
第二个选项是使用项目列表进行搜索。这可以通过将非应用程序名称移出参数,然后循环其余项目来完成:
proc shift {list} {
set result ""
upvar 1 $list ll
set ll [lassign $ll result]
return $result
}
set HOST [shift argv]
set USER [shift argv]
set PASSWORD [shift argv]
puts "leading args: >$HOST< >$USER< >$PASSWORD<"
set buff "blah bar blah"
foreach substr $argv {
if {[string match "*$substr*" $buff]} {
puts "match >$buff< on >$substr<"
}
}