2013-11-26 45 views
1

我试图编写一个带有一些嵌入式Expect脚本的小型bash脚本,以便在故障转移到备用站点时向路由器发送一些预定义的更改。
我期望的脚本以其最简单的形式工作。我遇到了添加一些错误检查的用法。Expect脚本 - 在比赛结束后停止评估并继续评估

我试图在juniper ssg路由器上调用“get policy id x”命令,然后匹配我们将启用或禁用的策略是正确的,然后继续检查VIP定义是否在事实上在这个路由器上运行,所有这些都是在我们做出改变之前。

的想到这个脚本的一部分去如下...

# start the ssh connection, end unless we get a shell prompt 
spawn ssh [email protected] 
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部分匹配,但随后移动到“发送‘获得VIP’命令,并继续评估最初的“get policy id”检查,而不是从get vip命令返回。

我该如何告诉期望停止评估并转到下一部分。

感谢

回答

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 "*-> $" 

Expect是块包含pattern {action}双。如果的动作缺少的最后一个模式,则期望块结束并且控制从下一个命令继续。

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 "*-> $"