I was wondering if anyone can give some advise/tips on this. My goal is to figure out the broadcast address. The way I think I should do is:
- get/detect the IP address and the subnet mask or CIDR
- convert the mask to binary form
- count the number of tailing 0s and apply the formula: m=2^n, where n is the number of the 0s
- replace the last bit of the IP address with the the number: m-1
and that should be the broadcast address.
My question is: what's the best way of getting [or detect] the IP address and subnet mask? Having said that, I mean which is OS independent. I can use "ifconfig" to figure out that info but again "ifconfig" implementation varies from distro to distro and definitely that not gonna work on windows. I tried using
socket.gethostbyname(socket.gethostname()) and also
socket.gethostbyname(socket.getfqdn()) but that doesn't return the correct IP address if the "/etc/hosts" file is not properly written. Otherwise it returns the loop back address. I'm stuck here. I'm sure there must be a better way of doing this. Anyone out here got something for me?
Thanks in advance. Cheers!!!