簡體   English   中英

AWS CLI - 管理實例的 Bash 腳本

[英]AWS CLI - Bash script to manage instances

我需要在 AWS 上運行 shadowsocks 服務器(我住在中國,這是使用 google 的最佳解決方案......)

我已經在我的 VM 實例上設置了所有內容,唯一的問題是當 VM 重新啟動時 ip 也會發生變化,因此我必須使用新 ip 手動設置我的 shadowsock 客戶端配置。

  • 如何使用 bash 腳本管理實例?
    例如獲取此信息:公共 ip、狀態或啟動/停止實例?

在您的系統上安裝了 AWS cli,您可以使用以下腳本作為示例。

獲取實例 ID
您可以獲得所有實例 ID 的列表(對於當前配置的區域):

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

這里的腳本

  • 將它放在一個文件中(例如aws_i.sh )並使其可執行。
  • 使用以下選項之一運行它status|start|stop|ip|shadow

最后一個shadow它只是用我的ec2實例的 ip 重寫了我的 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

試試下面的 repo,你會喜歡的。

它利用許多工具來降低在 AWS 雲中維護 VPN 服務的成本。 簡而言之:

  • AWS Config:用於捕獲實例更改。
  • AWS Lambda:用於應用更改。
  • Celery:用於在Shadowsocks節點上做心跳,並動態維護SS端口。
  • name.com API:用於將DNS記錄推送到DNS服務器。

設置完成后,您幾乎無需手動操作,甚至您還可以有計划的作業來替換 Shadowsocks 節點的 IP 地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM