簡體   English   中英

如何捕獲CLI啟動的EC2實例的IP地址以供后續使用?

[英]How do I capture the IP address of a CLI-started EC2 instance for subsequent use?

我經常需要從命令行啟動AWS EC2實例以使用SSH,並希望編寫一個簡短的腳本來執行此操作,但我仍然堅持最基本的步驟。

例如,我可以開始使用

aws ec2 start-instances --instance-ids i-84Sd8jdf 

並希望繼續抓取分配給實例的IP地址,並將其用作環境變量或腳本變量,以預先形成后續操作,如

ssh ubuntu@<theIP>

要么

scp ubuntu@<theIP>:~/soruce_stuff/* ~/dest_folder/

但我無法弄清楚如何從start-instances命令或其他命令發出的任何JSON獲取IP地址。

如何編寫EC2實例的啟動腳本,為要分配的IP配備,以及捕獲分配的IP地址以供后續使用?

在bash中啟動實例的示例(基於此優秀答案 ),腳本休眠(以秒為單位指定),並將公共IP地址保存到本地變量:

aws ec2 start-instances --instance-ids i-aaaa1111
sleep 10
ec2Address=$(aws ec2 describe-instances --instance-ids i-aaaa1111 --query "Reservations[*].Instances[*].PublicIpAddress" --output=text)

核實:

echo $ec2Address
11.1.111.111

亞馬遜提供等待條件,等待實例准備就緒,然后才能對其執行其他任務。 這是一個可能對你有幫助的例子

aws ec2 start-instances --instance-ids $instance_id
aws ec2 wait instance-running --instance-ids $instance_id
aws ec2 describe-instances --instance-ids $instance_id --output text|grep ASSOCIATION |awk '{print $3}'|head -1

暫無
暫無

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

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