Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- from pprint import pprint
- from time import sleep
- client = boto3.client('ec2')
- def tag_ip(eip_alloc_id, tag_value):
- tag = client.create_tags(Resources=[eip_alloc_id, ], Tags=[{'Key': 'Name', 'Value': tag_value}, ])
- if tag:
- return True
- else:
- return False
- def release_ip(ip_release_alloc, ip):
- for item in range(5):
- try:
- client.release_address(AllocationId=ip_release_alloc)
- print(f'released ugly ip {ip}')
- return True
- except Exception as e:
- print(str(e))
- sleep(0.5)
- return True
- def get_nice_ip():
- response = client.allocate_address(Domain='vpc')
- if response:
- # print(response)
- if 'PublicIp' in response:
- ip = response['PublicIp']
- alloc_id = response['AllocationId']
- ip_split = ip.split('.')
- if ip_split[2] == ip_split[3]:
- tag_ip(alloc_id, 'GOLD')
- print(f'found {ip}')
- elif '00' in ip_split[2] and '00' in ip_split[3]:
- tag_ip(alloc_id, 'SILVER')
- print(f'found {ip}')
- elif int(ip_split[3]) == 0:
- print(f'found {ip}')
- tag_ip(alloc_id, 'BRONZE MINUS')
- elif int(ip_split[2]) + 1 == int(ip_split[3]) or int(ip_split[2]) - 1 == int(ip_split[3]) or int(
- ip_split[2]) - 1 == int(ip_split[3]) or int(ip_split[3]) - 1 == int(ip_split[2]):
- print(f'found {ip}')
- tag_ip(alloc_id, 'BRONZE')
- else:
- release_ip(response['AllocationId'], ip)
- else:
- pprint(response)
- if __name__ == '__main__':
- while True :
- get_nice_ip()
Add Comment
Please, Sign In to add comment