1

I have two data frame (frame_Ipam and frame_now).

In frame now one column contains IP address and in frame we have two columns:

  • one contain the start address
  • the other one the end address of a network

First I want to know in witch range the IP address is frame_now is in. I already done it. I use a function call "check_ip" that return true or false if an address is between one start and one end.

Code:

def is_ipv4(string):
     try:
        ipaddress.IPv4Network(string)
        return True
     except ValueError:
        return False

def check_ip(addr,site_Id_SN,Type,start,end,name,site_id_I):
    dfObj = pd.DataFrame(columns=['Subnet_name','start','ip_addr','end','Site_Id_Ipam','Site_Id_SN','Type'])
    for i,j,k,l in zip(start,end,name,site_id_I):
        result=ipaddress.ip_address(i)<ipaddress.ip_address(addr)<ipaddress.ip_address(j)
        if result is True: 
            dfObj = dfObj.append({'Subnet_name':l,'start': i, 'ip_addr':addr, 'end': j,'Site_Id_Ipam':k,'Site_Id_SN':site_Id_SN,'Type':Type}, ignore_index=True)
        if result is False:
            continue
    return(dfObj)

for x,y,z in zip(frame_now.loc[:,'ip_address'],frame_now.loc[:,'site_id_SN'],frame_now.loc[:,'Type']):
    ad_s=Ipam.loc[:,'adresse_start']
    ad_e=Ipam.loc[:,'adresse_end']
    site_id_ipam= Ipam.loc[:,'site_id_IPAM']
    name=Ipam.loc[:,'subnet_name']
    is_ipv4(x)
    if is_ipv4(x)is True:
        print(check_ip(x,y,z, ad_s,ad_e,site_id_ipam,name))    
    if is_ipv4(x)is False:
        print(x,"n'est pas une addresse ip")

This code don't give me the real range (the smallest range). It give me all the range that contain the IP address. How to find the god subnet or do you know how to find a subnet address (for example find 172.18.0.0/16 knowing the start address and the end address).

kwoxer
  • 3,734
  • 4
  • 40
  • 70
  • Welcome to stack overflow! This is a good problem description, but we can't actually test anything to help solve it. It would be very helpful to see samples of your input and desired output to make a [mcve]. See [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for assistance – G. Anderson Mar 09 '20 at 17:54
  • https://docs.python.org/3/library/ipaddress.html might help. – wwii Mar 09 '20 at 18:36
  • Improved your title and the text on many cases. Please next time try to write more slowly. You had many issues in your text and it was hard to understand for me! – kwoxer Mar 10 '20 at 08:44

0 Answers0