0

I am trying to compare a router output to a list of IPs and then enter a command on the router to add any missing IPs. These are held in two different files.

I have tried using what has been suggested with this question, but it doesn't work how I need it to.

Here is what I have done so far in my script, but I haven't been able to figure out how to print the difference

with open("mikrotik_output.txt") as file1, open("bwtest-ip") as file2:
    ips_to_keep = file2.read().splitlines()
    router_lines = file1.read().splitlines()

if task=="add":
    invalid_input = False
    with open('output.txt', 'w') as file_out:
        for line in router_lines:
            if re.match(r"^\s*[0-9]", line):
                if any(ip in line for ip in ips_to_keep):
                    file_out.write(line + "\n")

    with open('output.txt') as file3:
        out = file3.read().splitlines()
        print((str(ips_to_keep)))
        line_found = any(ips in line for line in file3)
        if not line_found:
            file3.seek(0, io.SEEK_END)
            # add = net_connect.send_command('/ip firewall add address' + (str(line)) + ' disabled=yes list=bwtest')
            print('/ip firewall add address' + (str(line)) + ' disabled=yes list=bwtest')
        if line_found:
            print('You broke it')

If you need anything extra info just let me know
Cheers

ankou
  • 15
  • 3
  • Your code is unnecessarily complicated and mixes the reading of files with the searching for IPs and the processing of missing IPs. Do all the reading upfront and create simple lists that you can process later. – jarmod Dec 06 '21 at 15:56

0 Answers0