Python Forum

Full Version: Subnet Mask Ranges
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All

I am very new to python, and i have written something that grabs your IP and returns coverts it into the subnet mask and then give it a friendly name
What i am stuck on is have a range of subnet

Here is what i have done
#!/usr/bin/env python

import ipaddress
import socket

ip_to_name = {
    '10.21.32.0/24'  : 'Site1',
}


host_name = socket.gethostname() 
host_ip = socket.gethostbyname(host_name) 

net = ipaddress.ip_network(u'{0}/255.255.255.0'.format(host_ip), strict=False)

print(ip_to_name[str(net)])
Want i am trying to achive is the lookup to be something like this

ip_to_name = {
    '10.21.32.0/24' - '10.21.36.0/24  : 'Site1',
}
I am open to suggestions, or better ways of doing this

Many thanks