首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我正在尝试从一个刚启动的实例获取公网IP

我正在尝试从一个刚启动的实例获取公网IP
EN

Stack Overflow用户
提问于 2019-06-06 05:41:55
回答 1查看 58关注 0票数 0

我需要使用AWS CLI启动一个服务器,然后在一个脚本中使用ssh。我可以将实例id加载到一个变量中,但我需要重新格式化它以将其推送到AWS,然后获得公共IP。

代码语言:javascript
复制
#!/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。

EN

回答 1

Stack Overflow用户

发布于 2019-06-06 10:00:42

如果您希望在首次启动后在亚马逊EC2实例上安装软件,则应通过 Data提供脚本。

请参阅:Running Commands on Your Linux Instance at Launch - Amazon Elastic Compute Cloud

该脚本将以root身份运行。要调试该脚本,请查看:

代码语言:javascript
复制
/var/log/cloud-init-output.log

此技术被所有AWS用户使用,并且比您试图实现的方法可靠得多。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56468406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档