python - Only Search the Exact Match -
so, have long list (example truncated) values this:
derp = [[('interface_name', 'interface-1'), ('ip_address', '10.1.1.1'), ('mac_address', 'xx:xx:xx:xx:xx:xx')], [('interface_name', 'interface 2'), ('ip_address', '10.1.1.2'), ('mac_address', 'xx:xx:xx:xx:xx:xx')], [('interface_name', 'interface 3'), ('ip_address', '10.1.1.11'), ('mac_address', 'xx:xx:xx:xx:xx:xx')]]
i have function goes through massive list , pulls out match based on ip problem is, seems match on in last octet , not exact match.
findip = sys.argv[1] def arpint(arp_info): val in arp_info: if re.search(findip, str(val)): interface = val.pop(0) string = val print string, interface[1] arpint(derp)
so in above case, if findip = '10.1.1.1' come 10.1.1.1 , 10.1.1.11. i'd imagine there has way force input is...
don't use regular expression. string itself.
data = [[('interface_name', 'interface-1'), ('ip_address', '10.1.1.1'), ('mac_address', 'xx:xx:xx:xx:xx:xx')], [('interface_name', 'interface-1a'), ('ip_address', '010.001.001.001'), ('mac_address', 'xx:xx:xx:xx:xx:xx')], [('interface_name', 'interface 2'), ('ip_address', '10.1.1.2'), ('mac_address', 'xx:xx:xx:xx:xx:xx')], [('interface_name', 'interface 3'), ('ip_address', '10.1.1.11'), ('mac_address', 'xx:xx:xx:xx:xx:xx')]] key = '10.1.1.1' interface, ip, mac in data: if key in ip: #print(interface, ip) print([interface, ip, mac], interface[1])
of course works if ip addresses in data conform example ... no leading zeroes.
if addresses might have leading zeros compare integer equivalents of addresses
key = '10.1.1.1' key = map(int, key.split('.')) interface, ip, mac in data: ip_address = ip[1] ip_address = map(int, ip_address.split('.')) if ip_address == key: #print(interface, ip) print([interface, ip, mac], interface[1])
i don't have python 3.x on computer don't know if map objects can compared that. if not, use all(a == b a, b in zip(ip_address, key))
condition.
Comments
Post a Comment