How do I determine all of my IP addresses when I have multiple NICs?

Use the netifaces module. Because networking is complex, using netifaces can be a little tricky, but here’s how to do what you want: >>> import netifaces >>> netifaces.interfaces() [‘lo’, ‘eth0’] >>> netifaces.ifaddresses(‘eth0’) {17: [{‘broadcast’: ‘ff:ff:ff:ff:ff:ff’, ‘addr’: ’00:11:2f:32:63:45′}], 2: [{‘broadcast’: ‘10.0.0.255’, ‘netmask’: ‘255.255.255.0’, ‘addr’: ‘10.0.0.2’}], 10: [{‘netmask’: ‘ffff:ffff:ffff:ffff::’, ‘addr’: ‘fe80::211:2fff:fe32:6345%eth0’}]} >>> for interface in netifaces.interfaces(): … … Read more

Which MySQL datatype to use for an IP address? [duplicate]

Since IPv4 addresses are 4 byte long, you could use an INT (UNSIGNED) that has exactly 4 bytes: `ipv4` INT UNSIGNED And INET_ATON and INET_NTOA to convert them: INSERT INTO `table` (`ipv4`) VALUES (INET_ATON(“127.0.0.1”)); SELECT INET_NTOA(`ipv4`) FROM `table`; For IPv6 addresses you could use a BINARY instead: `ipv6` BINARY(16) And use PHP’s inet_pton and inet_ntop … Read more

How to check if an IP address is within a particular subnet

Take a look at IP Address Calculations with C# on MSDN blogs. It contains an extension method (IsInSameSubnet) that should meet your needs as well as some other goodies. public static class IPAddressExtensions { public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask) { byte[] ipAdressBytes = address.GetAddressBytes(); byte[] subnetMaskBytes = subnetMask.GetAddressBytes(); if (ipAdressBytes.Length != subnetMaskBytes.Length) … Read more

Most efficient way to store IP Address in MySQL [duplicate]

For IPv4 addresses, you may want to store them as an int unsigned and use the INET_ATON() and INET_NTOA() functions to return the IP address from its numeric value, and vice versa. Example: SELECT INET_ATON(‘127.0.0.1’); +————————+ | INET_ATON(‘127.0.0.1’) | +————————+ | 2130706433 | +————————+ 1 row in set (0.00 sec) SELECT INET_NTOA(‘2130706433’); +————————-+ | INET_NTOA(‘2130706433’) … Read more