If condition for comparing a string in Expect script

痞子三分冷 提交于 2019-12-10 18:38:21

问题


I have a expect script where I want to check uname -a.

if the remote server is Linux then I will run few more remote command using expect. If it is Aix then I will run few other set of commands. However I am not able to create the if condition. Can any one please help me?

expect <<'EOF'
spawn ssh user_name@server "uname -a"
set count 0
expect {
"password:" {
send password\r
exp_continue
}
"Permission denied" {
    incr count
    exp_continue
  }
}
set strname LINUX

set results $expect_out(buffer)

if {$strname == $results}
{
puts $results
}
EOF

回答1:


The uname -a command output will not just have the word Linux or Aix, but also the more info. So, it would be better to use regexp to check whether if it contains the word.

#!/bin/bash
expect <<'EOF'
spawn ssh dinesh@xxx.xxx.xx.xxx "uname -a"
set count 0
expect {
    timeout {puts "timeout happened"}
    eof {puts "eof received"}
    "password:" {send "password\r";exp_continue}
    "Permission denied" {incr count;exp_continue}
}

set results $expect_out(buffer)

if {[regexp -nocase "Linux" $results]} {
        puts "It is a Linux Machine"
} elseif {[regexp -nocase "Aix" $results]} {
        puts "It is a AIX machine"
} else {
        puts "Unknown machine"
}
EOF

Else, you should have used uname command alone which will only produce what is indeed needed to us. In that case, your given code will work fine.



来源:https://stackoverflow.com/questions/33174807/if-condition-for-comparing-a-string-in-expect-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!