Nikitenko Maxim (Sets88)

Personal page

Python - Duplicate IPs

I work as sysadmin, and eventualy, I found out, that there are some switches in my network which has same IP addresses.

And it's not cool at all, I can't access some of it, and the rest of them would not work well in particular situation.

But how can i find them, first thought were about arping.

And it's very hard to handle two /23 nets manualy, which has more than thousand addresses.

For this sutiation i wrote simple script which does entire work for me, here what it does:
- There are networks nets and related interfaces in "nets" dictionary
- Number of threads is in "threads_count" variable.
- Script just gets needed IP addresses one by another from given network, and sends ARP request using arping
- arping recives the reply
- If arping gets 2 replies, then it means something wrong, and it tells you about


Then you have to solve collisions having a list of IP addresses which script gave you.

Source code
#! /usr/bin/python
# -*- coding: utf-8 -*-
""" Check if there's dublicate IPs """

import ipaddr
import os
from multiprocessing import Pool

# Subnet and interface
nets = {"10.90.90.0/23": "eth1", "10.100.90.0/23": "eth1.110"}
# Multithreading
threads_count = 10

def get_response_count(ip, iface):
    proc = os.popen("arping %s -I %s -c1" % (str(ip), iface))
    text = proc.read().split("\n")
    return text[-2].split()[1]


def print_response(ipnet):
    response_count = int(get_response_count(ipnet[0], ipnet[1]))
    if response_count > 1:
        print "%d Responses got from ip %s on %s" % (response_count, ipnet[0], ipnet[1])


def ip_net_generator(iplist, net):
    for ip in iplist:
        yield (ip, net)


def main():
    pool = Pool(threads_count)
    for net in nets:
#        ips = [(x,y) for x in ipaddr.IPNetwork(net) for y in [nets[net]]]
        try:
            pool.map_async(print_response, ip_net_generator(ipaddr.IPNetwork(net), nets[net])).get(9999)
        except KeyboardInterrupt:
            pool.terminate()
            return


if __name__ == "__main__":
    main()
Яндекс.Метрика