lundi 24 août 2015

On AWS, how do I switch an elastic IP address and reconnect in Ruby?

I'm using script.rb to test out elastic IP on my AWS. I want to get a new IP address, switch to it reconnect to AWS with the new IP.

Here's script.rb:

require 'mechanize'
require 'json'
require 'pp'

(1..100).each{|x|
    agent = Mechanize.new
    agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Get around site's SSL problems
    p JSON.parse(agent.get('http://ift.tt/1ExhLr8').body)['ip']

    p x

    if(x!=50)
        next
    end

    describeAddresses = `aws ec2 describe-addresses`
    awsHash = JSON.parse(describeAddresses)
    pp awsHash

    p '========'

    getInstanceID = `wget -O - 'http://ift.tt/1Lw2fVN' 2>/dev/null`
    instanceID = getInstanceID.split("\n")[-1]
    pp instanceID
    pp '==='

    allocateIP = `aws ec2 allocate-address`
    allocateIPHash = JSON.parse(allocateIP)
    allocationID = allocateIPHash['AllocationId']
    pp allocateIPHash
    pp '==='

    p '========'

    associate = `aws ec2 associate-address --instance-id #{instanceID} --allocation-id #{allocationID}`
    pp associate

    p '========'
}

I connect to my AWS with XShell and run the code with ruby script.rb. The script switches the IP, but then I get kicked off. I guess I have to wait until XShell realizes it must connect to AWS with the new IP address before I can get back on.

How do I avoid getting kicked off when I switch IP?




Aucun commentaire:

Enregistrer un commentaire