-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawsPrivateIPs.py
More file actions
46 lines (34 loc) · 1.33 KB
/
awsPrivateIPs.py
File metadata and controls
46 lines (34 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
import re
import os
import sys
#usage
if len(sys.argv) is not 2:
print "Usage info: ....\n"
exit(1)
#all aws regions
awsRegions = ["us-east-1","us-east-2","us-west-1","us-west-2","ap-south-1","ap-northeast-2","ap-southeast-1","ap-southeast-2","ap-northeast-1","ca-central-1","eu-central-1","eu-west-1","eu-west-2","eu-west-3","sa-east-1"]
datafile="rawAwsPrivateIpList.txt"
def formatIps():
ips = []
with open('rawAwsPrivateIpList.txt','rw') as inputfile:
for line in inputfile:
if re.findall(r'[0-9]+(?:\.[0-9]+){3}', line):
line = re.sub(r'[^\d\.]','',line)
ips.append(line)
with open('PrivateIPList.txt','w+') as outfile:
for ip in ips:
outfile.write(ip + '\n')
try:
with open(datafile) as f:
print('%s exists\n' % datafile)
formatIps()
except IOError as e:
for region in awsRegions:
textString = 'echo ' + str(region) + ' >> rawAwsPrivateIpList.txt'
os.system(textString)
textString = 'aws ec2 describe-instances --query "Reservations[*].Instances[*].PrivateIpAddress" --profile ' + str(sys.argv[1]) + ' --region ' + str(region) + '>> rawAwsPrivateIpList.txt'
print 'Querying ' + region + '...'
os.system(textString)
formatIps()
print 'Raw IP results stored in "rawAwsPrivateIpList.txt".\nFormatted IP results stored in "PrivateIPList.txt".\n'