If condition for comparing a string in Expect scri

2019-07-03 22:28发布

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

标签: shell tcl expect
1条回答
家丑人穷心不美
2楼-- · 2019-07-03 22:54

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.

查看更多
登录 后发表回答