我正在使用XShell访问Linux AWS。我知道如何用Ruby切换弹性IP地址,但是当我这样做时,XShell与AWS的连接被破坏了。
这是script.rb
我通过XShell使用ruby script.rb
命令在AWS上运行:
require 'json'
require 'pp'
region = 'REGION NAME'
aws_access_key_id = 'SECRET KEY ID'
aws_secret_access_key = 'SECRET ACCESS KEY'
credentials = Aws::Credentials.new(aws_access_key_id, aws_secret_access_key)
client = Aws::EC2::Client.new(
region: region,
credentials: credentials
)
pp client
p '==='
describeAddresses = `aws ec2 describe-addresses`
awsHash = JSON.parse(describeAddresses)
pp awsHash
p '==='
getInstanceID = `wget -O - 'http://MY.AWS.IP.HERE/latest/meta-data/instance-id' 2>/dev/null`
instanceID = getInstanceID.split("\n")[-1]
pp instanceID
p '==='
resp = client.allocate_address({
domain: "vpc", # accepts vpc, standard
})
pp resp
p '==='
resp2 = client.associate_address({
instance_id: instanceID,
allocation_id: resp['allocation_id'],
allow_reassociation: true#,
})
pp resp2
p '==='