Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. """
  2. Requeriments:
  3. boto
  4. """
  5.  
  6. import time
  7. from boto.ec2.connection import EC2Connection
  8. from boto.route53.connection import Route53Connection
  9. from boto.route53.record import ResourceRecordSets
  10. from boto.exception import BotoClientError
  11.  
  12. AWS_ACCESS_KEY_ID = 'aws access key'
  13. AWS_SECRET_ACCSESS_KEY = 'aws secret key'
  14.  
  15. HOSTED_ZONE = 'route53 hosted zone id'
  16. DOMAIN_NAME = 'vpn domain name'
  17. VPC_DOMAIN = 'default vpc'
  18. LOG_FILE = '/var/log/ip_change.log'
  19.  
  20. VPN_INSTANCE_ID = 'instance id'
  21.  
  22. get_change_id = lambda response: response['ChangeInfo']['Id'].split('/')[-1]
  23. get_change_status = lambda response: response['ChangeInfo']['Status']
  24.  
  25.  
  26. def change_ip(new_ip):
  27. try:
  28. conn = Route53Connection(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCSESS_KEY)
  29. zone = conn.get_hosted_zone(HOSTED_ZONE)
  30.  
  31. response = conn.get_all_rrsets(HOSTED_ZONE, 'A', DOMAIN_NAME, maxitems=1)[0]
  32. old_ip = response.resource_records[0]
  33.  
  34. # Delete the old record, and create a new one.
  35. # This code is from route53.py script, the change record command
  36. changes = ResourceRecordSets(conn, HOSTED_ZONE, '')
  37. change1 = changes.add_change("DELETE", DOMAIN_NAME, 'A', response.ttl)
  38. for old_ip in response.resource_records:
  39. change1.add_value(old_ip)
  40. change2 = changes.add_change("CREATE", DOMAIN_NAME, 'A', response.ttl)
  41. change2.add_value(new_ip)
  42. commit = changes.commit()
  43. change = conn.get_change(get_change_id(commit['ChangeResourceRecordSetsResponse']))
  44. while get_change_status(change['GetChangeResponse']) == 'PENDING':
  45. time.sleep(2)
  46. change = conn.get_change(get_change_id(change['GetChangeResponse']))
  47. except BotoClientError:
  48. logging.exception(BotoClientError)
  49.  
  50.  
  51. def change_elastic_ip(id):
  52. try:
  53. conn = EC2Connection(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCSESS_KEY)
  54.  
  55. address = conn.allocate_address(domain=VPC_DOMAIN)
  56.  
  57. conn.associate_address(instance_id=id, allocation_id=address.allocation_id)
  58. addresses = conn.get_all_addresses()
  59. for addr in addresses:
  60. if addr.instance_id is None:
  61. conn.release_address(allocation_id=addr.allocation_id)
  62. # print('%s - %s' % (addr.public_ip, addr.allocation_id))
  63. logging.info(address.public_ip)
  64. return address.public_ip
  65. except BotoClientError:
  66. logging.exception(BotoClientError)
  67.  
  68.  
  69. if __name__ == '__main__':
  70. import logging
  71. import logging.handlers
  72.  
  73. FORMAT = "%(asctime)-15s %(message)s"
  74. logging.basicConfig(format=FORMAT, filename=LOG_FILE, level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
  75.  
  76. change_ip(change_elastic_ip(VPN_INSTANCE_ID))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement