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

Private IP Address Identifier in Regular Expression

I’m assuming you want to match these ranges: 127. 0.0.0 – 127.255.255.255 127.0.0.0 /8 10. 0.0.0 – 10.255.255.255 10.0.0.0 /8 172. 16.0.0 – 172. 31.255.255 172.16.0.0 /12 192.168.0.0 – 192.168.255.255 192.168.0.0 /16 You are missing some dots that would cause it to accept for example 172.169.0.0 even though this should not be accepted. I’ve fixed … Read more

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

How to enumerate IP addresses of all enabled NIC cards from Java?

This is pretty easy: try { InetAddress localhost = InetAddress.getLocalHost(); LOG.info(” IP Addr: ” + localhost.getHostAddress()); // Just in case this host has multiple IP addresses…. InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName()); if (allMyIps != null && allMyIps.length > 1) { LOG.info(” Full list of IP addresses:”); for (int i = 0; i < allMyIps.length; i++) { … Read more

Best way to create IPEndpoint from string

This is one solution… public static IPEndPoint CreateIPEndPoint(string endPoint) { string[] ep = endPoint.Split(‘:’); if(ep.Length != 2) throw new FormatException(“Invalid endpoint format”); IPAddress ip; if(!IPAddress.TryParse(ep[0], out ip)) { throw new FormatException(“Invalid ip-adress”); } int port; if(!int.TryParse(ep[1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port)) { throw new FormatException(“Invalid port”); } return new IPEndPoint(ip, port); } Edit: Added a version … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)