0

txt' which has lots of IP address in Lines as below

10.2.3.4
10.5.6.7
10.8.6.8
10.80.67.1
10.66.77.2
10.86.98.9

and another file b.txt

10.5.6.7
10.33.56.2
10.55.78.5
10.86.98.3

Now i want to write a code in python, so that b.txt IP is compare based on same on three octets with the IP address in a.txt file.

So that the output can be the one which is not in the a.txt, so the output will be below as this two IP address three octets don't match any in a.txt file

10.33.56.2
10.55.78.5

Thanks in advance.

file1 = open(r'a.txt', 'r') 
file2 = open(r'b', 'r')
FO = open(r'c.txt', 'w') 
data = file1.read() 
my_list = data.splitlines('10.') 
for i in my_list: 
    j = my_list[0:4] 
    print(j) 
Ludisposed
  • 1,709
  • 4
  • 18
  • 38
PSK
  • 1
  • 2
  • 6
  • 1
    What did you try so far ? – Nab Ilovich Jul 19 '17 at 08:53
  • file1 = open(r'a.txt', 'r') file2 = open(r'b,txt 'r') FO = open(r'c.txt', 'w') data = file2.read() my_list = data.splitlines('10.') for i in my_list: j = my_list[0:4] print(j) – PSK Jul 19 '17 at 08:57
  • 2
    Edit your question instead of posting you code as a comment – Ludisposed Jul 19 '17 at 08:57
  • Possible duplicate of [Check if item is in an array](https://stackoverflow.com/questions/11251709/check-if-item-is-in-an-array) – Ivan Jul 19 '17 at 09:02

2 Answers2

0

Put your IPs from a in a set:

ip_a = set(line.split(' ').strip())  # Or 3 first bytes if you prefer

Next for each IP in B

if ip_in_b not in ip_a:  # Or 3 first bytes
    print(ip_in_b)
Benjamin
  • 3,350
  • 4
  • 24
  • 49
  • Thanks Benjamin, I haven't tried this yet as my requirement is fulfilled by the code provided by Ludisposed. But now i am looking ithe options to find based on IP address OCTET...For example, like using 10.5.6. to search if any match in other list. – PSK Jul 20 '17 at 08:23
  • Byte means octet in my answer – Benjamin Jul 20 '17 at 08:54
0

1. Try to open files with with open ... as, because in your current code you only open them and not even close. This way it will automaticcely close when finished opening the file.

with open('a.txt','r') as f:
    output_a = [i for i in f]
with open('b.txt','r') as f:
    output_b = [i for i in f]

2. To be able to find if three octects are in the list of some other ip's you could do something like this:

# Testdata
output_a = ['1.1.2.9', '1.5.65.32']
output_b = ['1.2.57.1', '1.5.65.39']

# This will check if any subIP is in the list of other ip's and if not will append to result
# The rest I leave up to you ;)

# First solution
result = []
subIP = lambda x: ['.'.join(i.split('.')[:3]) for i in x]
for sub, full in zip(subIP(output_a), output_a):
    if not len([ip for ip in subIP(output_b) if sub == ip]):
        result.append(full)
print(result) 
# Will print: ['1.1.2.9']

# Second solution
# Some set operations will also work, but will leave you with a set of ip with 3 octets 
# afterwards you need to convert it back to full length if you want
print (set(subIP(output_a)) - set(subIP(output_b)))
# Will print {'1.1.2'}
Ludisposed
  • 1,709
  • 4
  • 18
  • 38
  • Thanks Ludisposed, it works to find out to get the IP list not in a.txt. But i have one more questions, do we have options to find based on IP address OCTET...For example, like using 10.5.6. to search if any match in other list. – PSK Jul 20 '17 at 08:21
  • 1
    If my answer was helpfull, please upvote and mark as answer. That way you earn some reputation and keep the site-content high – Ludisposed Jul 20 '17 at 08:49