AWS CLI - 用于管理实例的Bash脚本

时间:2017-03-07 06:50:28

标签: bash amazon-ec2 aws-cli

我需要在AWS上运行shadowsocks服务器(我住在中国,这是使用谷歌的最佳解决方案......)

我已经设置everything on my VM instance唯一的问题是当VM重启时也会更改ip,所以我必须使用新的ip手动设置我的shadowsock客户端配置。

  • 如何使用bash脚本管理实例?
    例如获取此信息:公共IP,状态或启动/停止实例?

2 个答案:

答案 0 :(得分:0)

在系统上安装AWS cli后,您可以使用以下脚本作为示例。

获取实例ID
您可以获取所有实例ID的列表(对于当前配置的区域):

aws ec2 describe-instances  --query "Reservations[].Instances[].InstanceId

此处为剧本

  • 将其放入文件(ex aws_i.sh)并使其可执行。
  • 使用以下选项status|start|stop|ip|shadow
  • 之一运行它

最后一个shadow它只是使用我的ec2实例< p>重写我的shadowsocks客户端配置

 #!/bin/bash

INSTANCE_ID="YOUR_INSTANCE"

function _log(){
    trailbefore=$2
    start=""
    if [ -z $trailbefore ] || [ $trailbefore = true ]
        then
        start=" - "
    fi

    printf "$start$1"
}

function run_command (){
    COMMAND=$1
    # note in the original parameter count as new parameter
    QUERY="$2 $3"
    OUTPUT="--output text"
    local result=$(eval aws ec2 $COMMAND --instance-ids $INSTANCE_ID $QUERY $OUTPUT)
    echo "$result"
}

function getStatus(){
    CMD="describe-instances"
    EXTRA="--query \"Reservations[].Instances[].State[].Name\""
    result=$(run_command $CMD $EXTRA)
    echo $result
}

function _checkStatus(){
    status=$(getStatus)
    if [ $status = "pending" ] || [ $status = "stopping" ]
        then
        _log "Current status: $status"
        _log " Wating "
        while [ $status = "pending" ] || [ $status = "stopping" ]
            do
            sleep 5
            _log "." false
            status=$(getStatus)
        done
        _log "\n" false
    fi
}

function getIp(){
    CMD="describe-instances"
    EXTRA="--query \"Reservations[].Instances[].PublicIpAddress\""
    _checkStatus

    if [ $status = "running" ]
        then
        ip=$(run_command $CMD $EXTRA)
        if [ -z "$ip" ]
            then
            _log "Wating for ip "
            while [ -z "$ip" ]
                do
                _log "." false
                sleep 5
                ip=$(run_command $CMD $EXTRA)
            done
        fi
        ## return value
        echo $ip
    else
        _log "Instance not runnning, please start it \n"
    fi

}

function start {
    CMD="start-instances"
    _checkStatus
    result=$(run_command $CMD)
    echo $result
}
function stop {
    CMD="stop-instances"
    _checkStatus
    result=$(run_command $CMD)
    echo $result
}

function shadow(){
    status=$(getStatus)
    if [ $status = "running" ]
        then
        _printConfig
    else
        _log "Not runnning, starting ...\n"
        start
        sleep 5
        shadow
    fi
}

function _printConfig(){
    IP=$(getIp)
    _log "Using IP: $IP\n"      
    FILE="/etc/shadowsocks/aws.json"
    cat >tmpfile.tmp <<EOL 
{
    "server":"$IP",
    "server_port":8000,
    "password":"PASS",
    "method":"aes-256-cfb",
    "local_address": "127.0.0.1",
    "local_port":1180,
    "timeout":300,
    "fast_open": false,
    "workers": 1,
    "prefer_ipv6": false
}   
EOL
    _log "Writing Config\n"
    sudo mv tmpfile.tmp $FILE 
    _log "Starting ShadowsSocks\n"
    sudo systemctl restart shadowsocks@aws
}

if [ -z "$1" ]
    then
    _log "\n Possible commands: status|start|stop|ip|shadow \n\n"
else
    if [ $1 = "start" ] 
        then
        start
    elif [ $1 = "stop" ] 
        then
        stop
    elif [ $1 = "status" ] 
        then
        getStatus   
    elif [ $1 = "ip" ]
        then
        getIp
    elif [ $1 = "shadow" ]
        then
        shadow
    fi
fi

答案 1 :(得分:0)

试试下面的 repo,你会爱上它。

它利用许多工具来降低在 AWS 云中维护 VPN 服务的成本。简而言之:

  • AWS Config:用于捕获实例更改。
  • AWS Lambda:用于应用更改。
  • Celery:用于在 Shadowsocks 节点上进行心跳,并动态维护 SS 端口。
  • name.com API:用于将 DNS 记录推送到 DNS 服务器。

设置完成后,您几乎无需手动操作,甚至可以有计划的工作来替换 Shadowsocks 节点的 IP 地址。

相关问题