This script will monitor another NAT instance and take over its routes if communication with the other instance fails

[root@ip-10 bin]# cat nat_monitor.sh
#!/bin/sh
# This script will monitor another NAT instance and take over its routes
# if communication with the other instance fails # NAT instance variables
# Other instance's ID to check and route to grab if other node goes down
NAT_ID=i-0273ed20b000000
NAT_RT_ID=rtb-000000a # My route to grab when I come back up
My_RT_ID=rtb-000000b # Specify the EC2 region that this will be running in (e.g. https://ec2.us-east-1.amazonaws.com)
EC2_URL=https://ec2.cn-north-1.amazonaws.com.cn # Health Check variables
Num_Pings=3
Ping_Timeout=1
Wait_Between_Pings=2
Wait_for_Instance_Stop=60
Wait_for_Instance_Start=300 # Run aws-apitools-common.sh to set up default environment variables and to
# leverage AWS security credentials provided by EC2 roles
. /etc/profile.d/aws-apitools-common.sh # Determine the NAT instance private IP so we can ping the other NAT instance, take over
# its route, and reboot it. Requires EC2 DescribeInstances, ReplaceRoute, and Start/RebootInstances
# permissions. The following example EC2 Roles policy will authorize these commands:
# {
# "Statement": [
# {
# "Action": [
# "ec2:DescribeInstances",
# "ec2:CreateRoute",
# "ec2:ReplaceRoute",
# "ec2:StartInstances",
# "ec2:StopInstances"
# ],
# "Effect": "Allow",
# "Resource": "*"
# }
# ]
# } # Get this instance's ID
Instance_ID=`/usr/bin/curl --silent http://169.254.169.254/latest/meta-data/instance-id`
# Get the other NAT instance's IP
NAT_IP=`/opt/aws/bin/ec2-describe-instances $NAT_ID -U $EC2_URL | grep PRIVATEIPADDRESS -m 1 | awk '{print $2;}'` echo `date` "-- Starting NAT monitor"
echo `date` "-- Adding this instance to $My_RT_ID default route on start"
/opt/aws/bin/ec2-replace-route $My_RT_ID -r 0.0.0.0/0 -i $Instance_ID -U $EC2_URL
# If replace-route failed, then the route might not exist and may need to be created instead
if [ "$?" != "0" ]; then
/opt/aws/bin/ec2-create-route $My_RT_ID -r 0.0.0.0/0 -i $Instance_ID -U $EC2_URL
fi while [ . ]; do
# Check health of other NAT instance
pingresult=`ping -c $Num_Pings -W $Ping_Timeout $NAT_IP | grep time= | wc -l`
# Check to see if any of the health checks succeeded, if not
if [ "$pingresult" == "0" ]; then
# Set HEALTHY variables to unhealthy (0)
ROUTE_HEALTHY=0
NAT_HEALTHY=0
STOPPING_NAT=0
while [ "$NAT_HEALTHY" == "0" ]; do
# NAT instance is unhealthy, loop while we try to fix it
if [ "$ROUTE_HEALTHY" == "0" ]; then
echo `date` "-- Other NAT heartbeat failed, taking over $NAT_RT_ID default route"
/opt/aws/bin/ec2-replace-route $NAT_RT_ID -r 0.0.0.0/0 -i $Instance_ID -U $EC2_URL
ROUTE_HEALTHY=1
fi
# Check NAT state to see if we should stop it or start it again
NAT_STATE=`/opt/aws/bin/ec2-describe-instances $NAT_ID -U $EC2_URL | grep INSTANCE | awk '{print $5;}'`
if [ "$NAT_STATE" == "stopped" ]; then
echo `date` "-- Other NAT instance stopped, starting it back up"
/opt/aws/bin/ec2-start-instances $NAT_ID -U $EC2_URL
NAT_HEALTHY=1
sleep $Wait_for_Instance_Start
else
if [ "$STOPPING_NAT" == "0" ]; then
echo `date` "-- Other NAT instance $NAT_STATE, attempting to stop for reboot"
/opt/aws/bin/ec2-stop-instances $NAT_ID -U $EC2_URL
STOPPING_NAT=1
fi
sleep $Wait_for_Instance_Stop
fi
done
else
sleep $Wait_Between_Pings
fi
done

add script to crontab, will run auto after booting

[root@ip-10 bin]# crontab -l
@reboot /root/bin/nat_monitor.sh >> /tmp/nat_monitor.log

AWS nat monitor and route switch script的更多相关文章

  1. AWS国际版的Route 53和CloudFront

    注册AWS国际版账号后,却发现Route 53和CloudFront功能是无法使用的.于是提交了一个Service Request,得到的答复是这两个功能需要验证后才能激活. 在控制台中点击进入Rou ...

  2. [转载]抓包,端口镜像,monitor session命令(转)

    原文地址:抓包,端口镜像,monitor session命令(转)作者:浮云皓月 一.SPAN简介 SPAN技术主要是用来监控交换机上的数据流,大体分为两种类型,本地SPAN和远程SPAN. --Lo ...

  3. 静态NAT、动态NAT

    静态NAT.动态NAT 实验拓扑: 实验目的:熟悉网络地址转换协议 掌握静态NAT 和动态NAT的配置 分析静态NAT 和动态NAT的区别 使用show命令来检查NAT的运行情况 实验要求:按拓扑图来 ...

  4. P4Lang Repository: Switch

    Github Switch Introduction Structure: +-----+ +-----+ +-----+ +-----+ |App a| |App j| |App n| |App z ...

  5. monitor.sh java脚本学习

    #! /bin/bash# unset any variable which system may be using# clear the screen while getopts ivh named ...

  6. react-router中,<switch>

    有<Switch>标签,则其中的<Route>在路径相同的情况下,只匹配第一个,这个可以避免重复匹配: 无<Switch>标签,则其中的<Route>在 ...

  7. juniper常用命令(二)

    Juniper防火墙基本命令 常用查看命令 Get int查看接口配置信息 Get int ethx/x查看指定接口配置信息 Get mip查看映射ip关系 Get route查看路由表 Get po ...

  8. juniper常用命令

    Juniper防火墙基本命令 get interface ethernet0/0  查看 端口 常用查看命令 Get int 查看接口配置信息 Get int ethx/x 查看指定接口配置信息  G ...

  9. 七天接手react项目 系列 —— react 路由

    其他章节请看: 七天接手react项目 系列 react 路由 本篇首先讲解路由原理,接着以一个基础路由示例为起点讲述路由最基础的知识,然后讲解嵌套路由.路由传参,最后讲解路由组件和一般组件的区别,以 ...

随机推荐

  1. 简单整理React的Context API

    之前做项目时经常会遇到某个组件需要传递方法或者数据到其内部的某个子组件,中间跨越了甚至三四层组件,必须层层传递,一不小心哪层组件忘记传递下去了就不行.然而我们的项目其实并没有那么复杂,所以也没有使用r ...

  2. Angularjs 跨域post数据到springmvc

    先贴网上己有解决方案链接: http://www.tuicool.com/articles/umymmqY  (讲的是springmvc怎么做才可以跨域) http://my.oschina.net/ ...

  3. Lucene如何实现多条件搜索?

    有两种方式可以实现, 一是:Lucene搜索API中提供了一个布尔查询器(BooleanQuery),它可以包含多个查询器,每个查询器Occur枚举控制是“and” 还是“or” BooleanQue ...

  4. Electron入门应用打包exe(windows)

    最近在学习nodejs,得知Electron是通过将Chromium和Node.js合并到同一个运行时环境中,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一门技术.对于之前一直从 ...

  5. 15 GIL 全局解释器锁 C语言解决 top ps

    1.GIL 全局解释器锁:保证同一时刻只有一个线程在运行. 什么是全局解释器锁GIL(Global Interpreter Lock) Python代码的执行由Python 虚拟机(也叫解释器主循环, ...

  6. 13 IO多路复用 (未完成)

    IO多路复用 6.select版-TCP服务器:最多1024 import select import socket import sys server = socket.socket(socket. ...

  7. Delphi7目录结构----初学者参考

    打开Delphi的安装目录,如C:\Program Files\Borland\Delphi7,你将会看到目录下包含了一些文件和文件夹: ²        Source:存放的是Delpi提供的所有源 ...

  8. 『JavaScript』封装

    封装可以被定义为对对象的内部数据表现形式和实现细节进行隐藏.通过封装可以强制实施信息隐藏. 在JavaScript中,并没有显示的声明私有成员的关键字等.所以要想实现封装/信息隐藏就需要从另外的思路出 ...

  9. Linux复制和移动文件

    cp:复制文件和目录 cp /etc/log /mu 把/etc/log复制到/mu目录下 -r:递归复制目录 -f:强制复制目录或文件 -i:交互式 -p:保留源文件或目录的属性 mv:移动或重命名 ...

  10. Asp.net core中Migration工具使用的交流分享

    a,ul>li,em{ color:deeppink !important; } h2>a{ text-decoration:none; } ul>li{ padding:3px; ...