I'm trying to make a script/program where I can find matching IP Addresses in two text files:
- One text file that contains a list of IP addresses (1.1.1.1)
- One text file that contains a list of subnets (1.1.1.0/28)
And I want to use regex and I'm not really sure how to do it.
Example:
import re
def check(fname1, fname2):
f2 = open(fname2)
f1 = open(fname1)
pattern = ('\d{1,3}\.\d{1,3}\.\d{1,3}')
for line in f1:
p1 = re.match(pattern, line)
out_p1 = p1.group(0)
for item in f2:
p2 = re.match(pattern, item)
out_p2 = p2.group(0)
if out_p1 in out_p2:
print(line, item)
So I'm trying to match an IP address from the first text file with a subnet from the second text file. Then I want to output the IP address with it's matching subnet.
Like so:
#IP #Subnet
1.1.1.1, 1.1.1.0/28
8.8.10.5, 8.8.8.0/23
F1in lines ofF2? What do you mean bycompare the patterns of the two files.patternfromF1, then you want to look for that match inF2, but your code looks for a match ofpatterninF2