我需要使用AWS CLI启动一个服务器,然后在一个脚本中使用ssh
。我可以将实例id加载到一个变量中,但我需要重新格式化它以将其推送到AWS,然后获得公共IP。
#!/bin/bash
azid=`(hidden for obvious reasons)`
azpswd=`(hidden for obvious reasons)`
hostip=`curl http://icanhazip.com`
COUNTER=120
#make a file to hold json information
touch instance.txt
#login to azure requireing webpage (not needed on cloud9)
#az login -u $azid -p $azpswd
#make a key pair and add to a variable (only needed one time)
#mykey=`aws ec2 create-key-pair --key-name MyKeyPair`
#open ports on security group only needed one time
#aws ec2 authorize-security-group-ingress --group-id sg-################# --protocol tcp --port 22 --cidr $hostip/32
#aws ec2 authorize-security-group-ingress --group-id sg-################# --protocol tcp --port 80 --cidr 0.0.0.0/0
#aws ec2 authorize-security-group-ingress --group-id sg-################# --protocol tcp --port 8080 --cidr 0.0.0.0/0
#aws ec2 authorize-security-group-ingress --group-id sg-################# --protocol tcp --port 32400 --cidr 0.0.0.0/0
#launch instance and add ouptut to variable
aws ec2 run-instances --image-id ami-024a64a6685d05041 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-################# --subnet-id subnet-################# > instance.txt
#pull instance id from instance.txt
inid=`grep "InstanceId" instance.txt`
#testing
echo $inid
#wait for vm to spin up
for ((i=COUNTER; i>=1; i--))
do sleep 1
echo $i
done
#get public ip from instance id
inip=`aws ec2 describe-instances --instance-ids $inid | grep PublicIpAddress`
#test
echo $inip
#ssh into freshly spun up server
ssh -i 'MyKeyPair.pem' ubuntu@$inip
从json文件中拉出一个实例ID,然后将其重新格式化以与aws ec2 describe-instances
一起使用,然后在服务器启动后拉下公共ip。然后,将ssh放入其中,并推送一个kickstart文件,以便新启动的服务器将在docker中运行plex。
发布于 2019-06-06 10:00:42
如果您希望在首次启动后在亚马逊EC2实例上安装软件,则应通过 Data提供脚本。
请参阅:Running Commands on Your Linux Instance at Launch - Amazon Elastic Compute Cloud
该脚本将以root
身份运行。要调试该脚本,请查看:
/var/log/cloud-init-output.log
此技术被所有AWS用户使用,并且比您试图实现的方法可靠得多。
https://stackoverflow.com/questions/56468406
复制相似问题