Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import boto3
- GROUP_ID = 'XXX'
- # ip = sys.argv[1]
- new_ip = sys.stdin.read()
- client = boto3.client('ec2', region_name='ap-northeast-1')
- res = client.describe_security_groups(GroupIds=[GROUP_ID])
- for p in res['SecurityGroups'][0]['IpPermissions']:
- current_ip = p['IpRanges'][0]['CidrIp'].split('/')[0]
- if new_ip == current_ip:
- print('%s already exists.' % new_ip)
- sys.exit()
- res = client.authorize_security_group_ingress(
- GroupId=GROUP_ID,
- IpPermissions=[
- {'IpProtocol': 'tcp',
- 'FromPort': 8888,
- 'ToPort': 8888,
- 'IpRanges': [
- {
- 'CidrIp': '%s/32' % new_ip,
- 'Description': 'tmp',
- }
- ],
- },
- {'IpProtocol': 'tcp',
- 'FromPort': 22,
- 'ToPort': 22,
- 'IpRanges': [
- {
- 'CidrIp': '%s/32' % new_ip,
- 'Description': 'tmp',
- }
- ],
- },
- ])
- print('%s added.' % new_ip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement