我正在尝试编写一个带有一些嵌入式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“命令,并继续评估初始”get policy id“检查而不是get vip命令的返回。
我如何告诉期望停止评估并继续下一部分。
谢谢
I'm attempting to write a small bash script with some embedded Expect scripting to send our router some predefined changes in the case of a failover to an alternate site. My expect script in its simplest form works. I'm running into uses in adding some error checking.
I'm attempting to call the "get policy id x" command on a juniper ssg router and then match that the policy we will be enabling or disabling is the correct one, then proceed to check that the VIP definitions are in fact running on this router, all before we make the changes.
The expect part of this script goes as follows…
# 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 "*- $" } } }The initial part of the script runs and matches on the VIP portion, but then moves to the 'send "get vip" command, and continues to evaluate the initial "get policy id" check instead of the return from the get vip command.
How do I tell expect to stop evaluating and move on to the next portion.
Thanks
最满意答案
您可能希望您的expect块看起来像这样:
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块结束,控制从下一个命令开始。
You probably want your expect blocks to look like this:
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 blocks contain pattern {action} pairs. If the action is missing for the last pattern then the expect block ends and control proceeds from the next command.
期待脚本 - 在比赛后停止评估并继续前进(Expect script - stop evaluating after match and move on)我正在尝试编写一个带有一些嵌入式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“命令,并继续评估初始”get policy id“检查而不是get vip命令的返回。
我如何告诉期望停止评估并继续下一部分。
谢谢
I'm attempting to write a small bash script with some embedded Expect scripting to send our router some predefined changes in the case of a failover to an alternate site. My expect script in its simplest form works. I'm running into uses in adding some error checking.
I'm attempting to call the "get policy id x" command on a juniper ssg router and then match that the policy we will be enabling or disabling is the correct one, then proceed to check that the VIP definitions are in fact running on this router, all before we make the changes.
The expect part of this script goes as follows…
# 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 "*- $" } } }The initial part of the script runs and matches on the VIP portion, but then moves to the 'send "get vip" command, and continues to evaluate the initial "get policy id" check instead of the return from the get vip command.
How do I tell expect to stop evaluating and move on to the next portion.
Thanks
最满意答案
您可能希望您的expect块看起来像这样:
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块结束,控制从下一个命令开始。
You probably want your expect blocks to look like this:
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 blocks contain pattern {action} pairs. If the action is missing for the last pattern then the expect block ends and control proceeds from the next command.
发布评论