如果条件用于比较Expect脚本中的字符串 [英] If condition for comparing a string in Expect script

查看:250
本文介绍了如果条件用于比较Expect脚本中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个期望脚本,我想在其中检查uname -a.

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

如果远程服务器是Linux,那么我将使用Expect运行更多的远程命令.如果是Aix,那么我将运行其他几组命令.但是,我无法创建if条件.谁能帮我吗?

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

推荐答案

uname -a命令输出将不仅包含单词LinuxAix,而且还有更多信息.因此,最好使用regexp检查它是否包含单词.

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

否则,您应该只使用uname命令,它只会产生我们确实需要的东西.在这种情况下,您给定的代码可以正常工作.

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.

这篇关于如果条件用于比较Expect脚本中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆