Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import sys
  2. import boto3
  3.  
  4. GROUP_ID = 'XXX'
  5.  
  6. # ip = sys.argv[1]
  7. new_ip = sys.stdin.read()
  8.  
  9. client = boto3.client('ec2', region_name='ap-northeast-1')
  10. res = client.describe_security_groups(GroupIds=[GROUP_ID])
  11. for p in res['SecurityGroups'][0]['IpPermissions']:
  12. current_ip = p['IpRanges'][0]['CidrIp'].split('/')[0]
  13. if new_ip == current_ip:
  14. print('%s already exists.' % new_ip)
  15. sys.exit()
  16. res = client.authorize_security_group_ingress(
  17. GroupId=GROUP_ID,
  18. IpPermissions=[
  19. {'IpProtocol': 'tcp',
  20. 'FromPort': 8888,
  21. 'ToPort': 8888,
  22. 'IpRanges': [
  23. {
  24. 'CidrIp': '%s/32' % new_ip,
  25. 'Description': 'tmp',
  26. }
  27. ],
  28. },
  29. {'IpProtocol': 'tcp',
  30. 'FromPort': 22,
  31. 'ToPort': 22,
  32. 'IpRanges': [
  33. {
  34. 'CidrIp': '%s/32' % new_ip,
  35. 'Description': 'tmp',
  36. }
  37. ],
  38. },
  39. ])
  40. print('%s added.' % new_ip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement