I wrote a script to pull down a list of aws tags and then read the last octect and tell me which one is the highest IP. For example. here is the list of tags that are returned:
['vlslabmc, 172.16.0.13/24', 'vlslabmc,172.16.0.5/24', 'vlslabmc,172.16.0.3/24', 'vlslabmc,172.16.0.12/24', 'vlslabmc,172.16.0.16/24', 'vlslabmc,172.16.0.6/24', 'vlslabmc,172.16.0.1/24', 'vlslabmc,172.16.0.11/24', 'vlslabmc,172.16.0.15/24', 'vlslabmc,172.16.0.17/24', 'vlslabmc,172.16.0.4/24', 'vlslabmc,172.16.0.7/24', 'vlslabmc,172.16.0.10/24', 'vlslabmc,172.16.0.9/24', 'vlslabmc,172.16.0.8/24', 'vlslabmc,172.16.0.2/24', 'vlslabmc,172.16.0.14/24']
Here's my code to workout the largest IP from the tagLis (note that the largest is 17, 172.16.0.17)
21 def findLargestIP():
22 for i in tagList:
23 #remove all the spacing in the tags
24 ec2Tags = i.strip()
25 #seperate any multiple tags
26 ec2SingleTag = ec2Tags.split(',')
27 #find the last octect of the ip address
28 fullIPTag = ec2SingleTag[1].split('.')
29 #remove the CIDR from ip to get the last octect
30 lastIPsTag = fullIPTag[3].split('/')
31 lastOctect = lastIPsTag[0]
32 ipList.append(lastOctect)
33 largestIP = int(ipList[0])
34 for latestIP in ipList:
35 if int(latestIP) > largestIP:
36 largestIP = latestIP
37 return largestIP
I'm not sure why.. but when I print the value of largestIP it always prints out 16. Ive gone through the code it should have worked (I'm avoiding using the max function as I'm just learning to code)
Any help as aways is greatly appreciated.
Thanks
Edit with the answer below, and a question
Ok so thanks to a clue from cmarie I got it working the problem was mainly
33 largestIP = int(ipList[0])
Here's the code running before with an added print statement on the list append:
'13']
['13', '5']
['13', '5', '3']
['13', '5', '3', '12']
['13', '5', '3', '12', '16']
16
['13', '5', '3', '12', '16', '6']
16
['13', '5', '3', '12', '16', '6', '1']
16
['13', '5', '3', '12', '16', '6', '1', '11']
16
... ...
['13', '5', '3', '12', '16', '6', '1', '11', '15', '17', '4', '7', '10', '9', '8', '2']
16
['13', '5', '3', '12', '16', '6', '1', '11', '15', '17', '4', '7', '10', '9', '8', '2', '14']
16
Basically what was happening is that during this loop :
33 largestIP = int(ipList[0])
34 for latestIP in ipList:
35 if int(latestIP) > largestIP:
36 largestIP = latestIP
The loop stops at the 1st largest integer. in this case that is 16. *I'm not sure why it does but it does
Here's the working code:
19 def findLargestIP():
20 ipList =[]
21 for i in tagList:
22 #remove all the spacing in the tags
23 ec2Tags = i.strip()
24 #seperate any multiple tags
25 ec2SingleTag = ec2Tags.split(',')
26 #find the last octect of the ip address
27 fullIPTag = ec2SingleTag[1].split('.')
28 #remove the CIDR from ip to get the last octect
29 lastIPsTag = fullIPTag[3].split('/')
30 lastOctect = lastIPsTag[0]
31 ipList.append(int(lastOctect))
32 print ipList
33 largestIP = 0
34 for latestIP in ipList:
35 if latestIP > largestIP:
36 largestIP = latestIP
37 print latestIP
38 print largestIP
39 return largestIP
and the result:
[13, 5, 3, 12, 16]
13
16
[13, 5, 3, 12, 16, 6]
13
16
[13, 5, 3, 12, 16, 6, 1]
13
16
[13, 5, 3, 12, 16, 6, 1, 11]
13
16
[13, 5, 3, 12, 16, 6, 1, 11, 15]
13
16
[13, 5, 3, 12, 16, 6, 1, 11, 15, 17]
13
16
17
Note it found 17.