是否可以使用您所在的服务器更改 ec2 中的 IP?
我当前在控制台中执行的操作是:
但是,由于 ec2 服务器正在关闭,我正在控制台中执行此操作。有没有办法在我正在使用的当前服务器上执行此操作。例如,在伪代码中:
import ec2, boto
ec2.disappociate_current_ip()
ec2.release_ip()
ec2.allocate_new_ip()
ec2.associate_new_ip()
这是可能的。 但是 一旦您解除弹性 IP 地址的关联,您可能会失去互联网连接,具体取决于您的子网设置。如果您的子网配置为自动分配公共 IP,您将在取消关联和关联之间获得一个公共 IP(非弹性 IP)。但是,如果您的公有子网未配置为自动获取公有 IP,您的实例将失去互联网连接(除非有到达互联网的路由),并且脚本的其余部分将不会执行。以下是 Boto3 脚本,可为您提供一个想法但未经测试。调整它以满足您的需求。
import boto3
import requests
client = boto3.client('ec2')
inst_id = requests.get('http://169.254.169.254/latest/meta-data/instance-id').text
print inst_id
public_ip = requests.get('http://169.254.169.254/latest/meta-data/public-ipv4').text
print 'Current IP:', public_ip
print 'Disassociating:', public_ip
client.disassociate_address(PublicIp=public_ip)
public_ip = client.allocate_address( Domain='vpc')['PublicIp']
print 'New IP:', public_ip
print 'Associating:', public_ip
client.associate_address(InstanceId=inst_id, PublicIp=public_ip)
我在 AWS 知识中心找到了答案,说您可以通过他们的 web 控制台或命令行来完成此操作:https://repost.aws/knowledge-center/ec2-move-elastic -ip
使用 EC2 控制台:
打开 Amazon EC2 控制台,然后选择弹性 IP。
选择您要转移的弹性IP地址。
验证关联 ID 和关联实例 ID,以确认弹性 IP 地址当前与哪个实例关联。
选择操作,取消关联弹性 IP 地址。
选择取消关联。
再次选择弹性 IP 地址,然后选择操作、关联弹性 IP 地址。
选择实例,然后搜索要与弹性 IP 地址关联的实例的实例 ID。
注意:如果您的 EC2 实例上有多个网络接口,请执行以下操作:
选择网络接口。
选择您想要与弹性 IP 地址关联的弹性网络接口 ID。