Ipaddr = struct.unpack('>L',socket.inet_aton(ip)) Here's the result: def addressInNetwork(ip,net): In order to get other masks (/23, /21) working correctly I added a "greater than" to the struct commands and changed the code for creating the binary mask to start with all "1" and shift left by (32-mask).įinally, I added a simple check that the network address is valid for the mask and just print a warning if it is not. I also noticed that just ignoring the Endian behaviour assuming that consistency will save you will only work for masks on octet boundaries (/24, /16). Not ANDing the IP address with the Network address as was done. Most fundamentally - a match should be checked by ANDing the IP address with the mask, then checking the result matched the Network address exactly. I tried Dave Webb's solution but hit some problems: That's it, this is much faster than the solutions above with the included modules. IpNetworkInt = ipToInt(ipNetwork) #convert the ip network into integer formīinString = "".format(ipNetworkInt) #convert that into into binary (string format)ĬhopAmount = 0 #find out how much of that int I need to cut off IpInt = ipToInt(ip)#my test ip, in int form This job only requires simple math, so here is my simple function to do the job: def ipToInt(ip): I'm not a fan of using modules when they are not needed. Netmask = struct.unpack('L',socket.inet_aton(netaddr)) & ((2L<<int(bits)-1) - 1) Ipaddr = struct.unpack('L',socket.inet_aton(ip)) If you just want a single function that takes strings it would look like this: import socket,struct Return dottedQuadToNum(ip) & makeMask(bits) "Convert a network address to a long integer" Return struct.unpack('L',socket.inet_aton(ip)) "convert decimal dotted quad string to long integer" "return a mask of n bits as a long integer" I added a little to the article as follows: import socket,struct Minimal system required for WOL: OS X 10.This article shows you can do it with socket and struct modules without too much extra effort.Put devices remotely to sleep, shutdown or restart (Macintosh only).Wake devices remotely in your local network or via internet.See the connection quality of the connected wifi clients in realtime.Find detailed information about your Airport base stations like MAC address, IP's, used channels and so on.Monitor network traffic on Airport devices.See the current condition of Apple Airport base stations like Airport express or Time Capsule. The following Airport monitoring features can only be used with models prior to the 802.11ac: All other features of iNet are working properly.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |