Python Forum
OS Independent way to get network mask
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OS Independent way to get network mask
#1
I have an application that needs to send a broadcast message on the same network as my computer. Therefore I (think I) want to read the network mask from the interface so I know how to correctly set the subnet bits and the broadcast bits.
The application (a driver for weewx) can run on a wide variety of platforms.
I've seen various examples of how to do this by spawning off a subprocess and then parsing the output of the 'ifconfig' or 'ipconfig' or some other program that always seems  to be OS specific.
I want to make as few assumptions about the environment as possible. All I want to assume is that the device I want to broadcast to is on the same subnet as the computer I'm running on.
Pointers to some documentation that I have obviously missed or a short code example would be appreciated.
Susan
Reply
#2
Hello!
See ipaddress module
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Could you not just query your "host" file (provided you use it, of course  Smile )
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
@wavic - the way I read that module documentation you start by accessing an interface (if_interface) or network (if_network) and passing it the IP subnet address that you want. In my case the code could be running on any computer with whatever network setup the user wants - I don't know that beforehand. It could be 192.168.x.x or 10.x.x.x or anything else the user wants, especially if they are on a subnet that cannot be routed to the Internet. Therefore I don't see how to use that package without knowing the IP address AND the subnet mask (i.e. something like the "192.0.2.0/24" given in the examples - if I know the that then I'd already know the subnet mask).

@sparkz_alot - I want to make as few assumptions or preconditions as possible. Because this can run on whatever configuration the user wants, I can't assume there is a hosts file or that it is set up properly.

Susan
Reply
#5
I'm not sure, but I think you can use the ipaddress package: https://pypi.python.org/pypi/ipaddress/1.0.18
Instructions here: https://docs.python.org/3/howto/ipaddress.html
Reply
#6
I am not sure if you can get the network settings with just one call for any OS
Try this module: https://pypi.python.org/pypi/netifaces
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
I've never tried it, but you could possibly use the broadcast ip of 255.255.255.255 (which is for the 0.0.0.0 network which is your network). Note this never gets routed. You could also try using the Data Link layer of Ethernet by sending the broadcast to MAC FF:FF:FF:FF:FF:FF.

If you are using IPV6, all bets are off cuz it uses "multicast" instead of "broadcast".

Another avenue you might try is to search PyPI for packages related to ARP and see if any of them look promising.

EDIT:
I should also add, the broadcast address is almost always 255 as in 192.168.1.255
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
Larz60+ - Thanks for the suggestion. I looked at the same place when wavic suggested it but there are the issues I described earlier.
Wavic - I was trying to get away form a dependency on another package but I'll check this out further as it could well be the simplest approach for the end user (if they can install the driver for the app then they should have the skills for adding the package)
Sparks_alot - I've seen that "original style" of broadcast mask (as I've seen it called) but I must admit I've not tried to use it. As for the part in your "edit": I know the '255' is special but the question comes with subnets such as 10. addressing - how many bits are there in the subnet. Some could be 10.0.0.255 but others could be 10.255.255.255 etc..
The frustrating thing is that the interface knows the mask as it was either set statically or via DHCP. Given the constraints I'm working under, I was hoping this would be easier and I had just missed using some "magic term" in my Google searches.
Thanks all for the suggestions.
Susan
Reply
#9
Let's remember that the subnet mask is only used to separate the 'network' part of the address from the 'host' part of the address. It doesn't matter if the address is 10.x.x.x or 192.x.x.x . If I have the addresses 10.1.2.3 (A) and 10.1.2.4 (B) with a subnet mask of 255.255.255.0 we are saying that 10.1.2 is the network portion and .3 and .4 are the hosts portion and they can talk directly since they are on the same network and applies to the whole range 10.1.2.0 thru 10.1.2.255. If we change the address of B to 10.1.255.4, they are no longer part of the same network and so the broadcast address (the last octet being 255) will be different for both, one for each network.  If you change the subnet mask to 255.255.0.0, A and B are once again in the same network (10.1) with the same broadcast address.  By convention, a final octet of '0' is the entire network and the final octet of '255' is the broadcast address (I will add that, though not etched in stone, many network admins will use the final octet of '1' for the primary router).

Sorry to be long winded, but I wanted to make sure that current and future readers can have an understanding of what we are talking about here.

Back to your situation, if all you want to do is broadcast to the subnet (or 'hosts') you simply have to ensure the final octet = 255.  You shouldn't need to know the subnet mask of each computer. That has already been designated.  What you really need is the IP address of the computer running your script, change the final octet to 255 and your done.  Unless your script is building or re-building the network.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020