期待脚本 - 在比赛后停止评估并继续前进

时间:2013-11-26 20:17:44

标签: expect

我正在尝试编写一个带有一些嵌入式Expect脚本的小型bash脚本,以便在故障转移到备用站点时向我们的路由器发送一些预定义的更改。
我希望脚本以最简单的形式工作。我正在尝试添加一些错误检查。

我试图在juniper ssg路由器上调用“get policy id x”命令然后匹配我们将启用或禁用的策略是正确的,然后继续检查VIP定义是否实际上在我们进行更改之前,在此路由器上运行。

此脚本的期望部分如下...

# start the ssh connection, end unless we get a shell prompt
spawn ssh netscreen@duluthfw
expect {
    timeout  { send_user "\n Failed to get login prompt\n"; exit 1 }
    eof { send_user "\nSSH failure for hostname\n"; exit 1 }
    "*-> $"
    }
#Lets make sure this is the right policy, else lets check again

send "get policy id 9\r"
expect {
    default { send_user "\n This policy does not match the policy we need to enable, please look and set the POLICYNUM variable to the correct policy to enable.\n"; exit 1 }
    "VIP"  
    expect "*-> $"
        }   
send "get vip \r"
expect { 
default { send_user "These are not the VIP IPs or ports that we need to disable,  please verify this is being run against the correct router or that the failover has not already been implemented."; exit 1 }
        "HTTP" 
        send "unset interface wireless0/1 vip 192.168.35.3 port 80 \r"
        expect "*->"
        send "unset interface wireless0/1 vip 192.168.35.3 port 143 \r"
        expect "*- $"
        }
    }
}

脚本的初始部分在VIP部分上运行并匹配,但随后转到“send”get vip“命令,并继续评估初始的”获取策略ID“检查,而不是从获取vip返回命令。

我如何判断期望停止评估并转到下一部分。

由于

2 个答案:

答案 0 :(得分:0)

你可能希望你的期望块看起来像这样:

send "get policy id 9\r"
expect {
    default {
        send_user "\n This policy does not match the policy we need to enable, please look and set the POLICYNUM variable to the correct policy to enable.\n"
        exit 1 
    }
    "VIP"  
}
expect "*-> $"

send "get vip \r"
expect { 
    default {
        send_user "These are not the VIP IPs or ports that we need to disable,  please verify this is being run against the correct router or that the failover has not already been implemented."
        exit 1
    }
    "HTTP" 
}
expect "*-> $"
send "unset interface wireless0/1 vip 192.168.35.3 port 80 \r"
expect "*-> $"
send "unset interface wireless0/1 vip 192.168.35.3 port 143 \r"
expect "*-> $"

期望块包含pattern {action}对。如果最后一个模式的缺少操作,则expect块结束,控制从下一个命令开始。

答案 1 :(得分:0)

谢谢。就是这样。我的代码现在看起来像这样,并且正在运行。

send "get policy id 9 \r"
expect {
    default {
    send_user "\n WRONG POLICY This policy does not match the policy we need to enable, please look and set the POLICYNUM variable to the correct policy to enable.\n"
    exit 1
    }
    "VIP(192.168.35.3)"
}

send "get policy id 9 \r"
expect {
    default {
    send_user "\n NOT ENABLED This policy does not match the policy we need to enable, please look and set the POLICYNUM variable to the correct policy to enable.\n"
    exit 1
    }
    "enabled"
}

expect "*-> $"
send "set policy id 9 disable \r"
expect "*-> $"
send "get vip \r"
expect {
    default {
     send_user "These are not the VIP IPs or ports that we need to disable, please verify this is being run against the correct router or that the failover has not already been implemented."
      exit 1
      }
 "IMAP"
 }

 expect "*-> $"
 send "unset interface wireless0/1 vip 192.168.35.3 port 80 \r"
 expect "*-> $"
 send "unset interface wireless0/1 vip 192.168.35.3 port 143 \r"
 expect "*-> $"