AWS CLI-用于管理实例的Bash脚本
我需要在AWS上运行shadowsocks服务器(我住在中国,这是使用google的最佳解决方案…) 我已经设置了唯一的问题是当VM重新启动时,ip也会发生变化,因此我必须手动使用新的ip设置我的shadowsock客户端配置AWS CLI-用于管理实例的Bash脚本,bash,amazon-ec2,aws-cli,Bash,Amazon Ec2,Aws Cli,我需要在AWS上运行shadowsocks服务器(我住在中国,这是使用google的最佳解决方案…) 我已经设置了唯一的问题是当VM重新启动时,ip也会发生变化,因此我必须手动使用新的ip设置我的shadowsock客户端配置 如何使用bash脚本管理实例? 例如,获取以下信息:公共ip、状态或启动/停止实例 在系统上安装AWS cli后,可以使用以下脚本作为示例 获取实例id 您可以获得所有实例ID的列表(针对当前配置的区域): 这里是脚本 将它放在一个文件中(exaws_i.sh)并使其
- 如何使用bash脚本管理实例?
例如,获取以下信息:公共ip、状态或启动/停止实例
您可以获得所有实例ID的列表(针对当前配置的区域): 这里是脚本
- 将它放在一个文件中(ex
)并使其可执行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
#/bin/bash
实例\u ID=“您的\u实例”
函数_log(){
先行者=2美元
start=“”
如果[-z$trailbefore]| |[$trailbefore=true]
然后
start=“-”
fi
printf“$start$1”
}
函数run_命令(){
命令=$1
#注意,在原始参数中,计数为新参数
QUERY=“$2$3”
OUTPUT=“--输出文本”
本地结果=$(eval aws ec2$命令--实例ID$实例ID$查询$输出)
回显“$result”
}
函数getStatus(){
CMD=“描述实例”
EXTRA=“--query\”保留[]。实例[]。状态[]。名称“”
结果=$(运行命令$CMD$EXTRA)
echo$结果
}
函数_checkStatus(){
状态=$(getStatus)
如果[$status=“pending”][$status=“stopping”]
然后
_记录“当前状态:$status”
_日志“Wating”
而[$status=“pending”][$status=“stopping”]
做
睡眠5
_日志“.”假
状态=$(getStatus)
完成
_日志“\n”错误
fi
}
函数getIp(){
CMD=“描述实例”
EXTRA=“--query\”保留[]。实例[]。PublicIpAddress“”
_检查状态
如果[$status=“running”]
然后
ip=$(运行命令$CMD$EXTRA)
如果[-z“$ip”]
然后
_日志“等待ip”
而[-z“$ip”]
做
_日志“.”假
睡眠5
ip=$(运行命令$CMD$EXTRA)
完成
fi
##返回值
echo$ip
其他的
_日志“实例未运行,请启动它\n”
fi
}
功能启动{
CMD=“启动实例”
_检查状态
结果=$(运行命令$CMD)
echo$结果
}
功能停止{
CMD=“停止实例”
_检查状态
结果=$(运行命令$CMD)
echo$结果
}
函数阴影(){
状态=$(getStatus)
如果[$status=“running”]
然后
_打印配置
其他的
_日志“未运行,正在启动…\n”
开始
睡眠5
阴影
fi
}
函数_printConfig(){
IP=$(getIp)
_日志“使用IP:$IP\n”
FILE=“/etc/shadowsocks/aws.json”
cat>tmpfile.tmp试试下面的回购协议,你会喜欢的
它利用许多工具来降低在AWS云中维护VPN服务的成本。简言之:
- AWS配置:用于捕获实例更改
- AWS Lambda:用于应用更改
- 芹菜:用于在Shadowsocks节点上执行心跳,并动态维护SS端口
- name.com API:用于将DNS记录推送到DNS服务器
一旦设置好,您几乎可以免提,甚至您也可以有一个计划作业来替换Shadowsocks节点的IP地址。只需Google aws clipip安装awscli即可获得cli。请使用它。抱歉,我写这个问题只是为了分享我已经实现的解决方案
#!/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