If condition for comparing a string in Expect script
19,981
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 [email protected] "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.
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
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