I am creating custom code for fetching the IP and some other needed details from text file.
Consider the textfile is having following content: tenant_id and IP
cbdf25542c194a069464f69efff4859a 45.45.45.45
cbdf25542c194a069464f69efff4859a 1.6.7.3
cbdf25542c194a069464f69efff4859a 1.7.6.2
1235b3a73ad24b9c86cf301525310b24 2.3.7.5
1235b3a73ad24b9c86cf301525310b24 6.5.2.1
Now I have already created the code for fetching the IP and tenant separately.
Code is as follows:
files = open("/root/flattext", "r")
# create an empty list
ips = []
tenants = []
# read through the files
for text in files.readlines():
# strip off the \n
text = text.rstrip()
# IP and Tenant Fetch
regex = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})$', text)
regex1 = re.findall(r'[0-9A-Za-z]{32}', text)
if regex is not None and regex not in ips:
ips.append(regex)
if regex1 is not None and regex1 not in tenants:
tenants.append(regex1)
ip_valuess = [''.join(ip) for ip in ips if ip]
tenant_ids = [''.join(tenant) for tenant in tenants if tenant]
# cleanup and close files
files.close()
So It will be giving result which consists of IP and Tenant_id as separate list.
Here what I need is fetching the IP which is coming under particular tenant ID.
Consider 1235b3a73ad24b9c86cf301525310b24 as a tenant_id, So it should be giving result as 2.3.7.5 , 6.5.2.1.
Some one please have a look and give me a better way to sort it out.