Search code examples
pythondebuggingarp

How do you search each line in a file for all contents of a list?


I'm still new to Python and having some issues searching a file for the contents of a list. What I'm needing is to search an entire file for each string in a list. So I need to iterate through each element of a list for each line in a file and look for matches. If it helps, I'm trying to match mac addresses with contents of an arp table. The macs I'm looking for are in a list. The entire arp table is in a file.

Here's my code that is not working:

mac_addr = []  
ipaddr = []

with open('arp_table_output.txt','r+') as myArps:
    for line in myArps:
        val = line.split()
        for x in mac_addr:
            if x in line:
                ipaddr.append(val[0])

Here's an example of a line in the arp file:

10.10.10.4     00:18:32   38ea.a792.1e62  Dynamic    ARPA  Bundle-Ether2.3

Here's a snippet of what mac_addr looks like:

 0100.0ccc.cccc  
 0100.0ccc.cccd  
 0180.c200.0000  
 0180.c200.0001  
 0180.c200.0002  
 0180.c200.0003  
 0180.c200.0004  
 0180.c200.0005  
 0180.c200.0006  
 0180.c200.0007    
 0180.c200.0008  
 0180.c200.0009  

Solution

  • what I'd advice you would be to first remove all unnecessary data from the line, so you get exactly what you're looking for:

    addr = [ x for x in line.split(" ") if x ][2]
    

    and then printout that value along with the values in your list:

    for target in mac_addr:
        print("'{}' vs '{}' -> {}".format(addr, target, addr == target))
    

    so you can check how they differ.

    In the end:

    with open('arp_table_output.txt','r+') as myArps:
        for line in myArps:
            # select *ONLY* the lines that contain the IP and MAC information in the input file
            if "Dynamic" in line:
                # here we extract the IP and the MAC from the line (python3 syntax)
                ip, _, mac, *_ = [ x for x in line.split(" ") if x ]
                # if you prefer python2: use `x, _, y, _, _, _ = …`
    
                # Test to printout what works and what does not. If none pass, the "in" test below will fail.
                for target in mac_addr:
                    print("'{}' vs '{}' -> {}".format(mac, target, mac == target))
    
                if mac in mac_addr:
                    ipaddr.append(ip)