mercredi 2 septembre 2015

How do I build a Ruby scraper on AWS that uses AWS ec2 to switch elastic IP addresses?

I have a Ruby script set up on a Linux AWS box. I want the script to switch IP addresses every so often. I connect to the AWS with XShell.

Here's the part of my script that changes the AWS IP.

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://ift.tt/1IpSH7i' 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 '==='

Of course, this disconnects XShell from the AWS. I'm guessing there's a different way to accomplish what I want to do: Have a scraper running and change the AWS IP address every so often.

I hear there's a way to do this by having a daemon watching over everything, but I'm not sure how that would need to be set up.




Aucun commentaire:

Enregistrer un commentaire