Groovy IsIpAddressLocal


/ Published in: Groovy
Save to your folder(s)

See: // http://stackoverflow.com/questions/2406341/how-to-check-if-an-ip-address-is-the-local-host-on-a-multi-homed-system


Copy this code and paste it in your HTML
  1. public static boolean isInetAddressLocal(InetAddress addr) {
  2. // http://stackoverflow.com/questions/2406341/how-to-check-if-an-ip-address-is-the-local-host-on-a-multi-homed-system
  3. if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) { return true; }
  4. // Check if the address is defined on any interface
  5. try { return NetworkInterface.getByInetAddress(addr) != null; } catch (SocketException e) { return false;}
  6. }
  7.  
  8. public static boolean isIpStringAddressLocal(String strIpAddress) {
  9. def addr = InetAddress.getByName( strIpAddress );
  10. // http://stackoverflow.com/questions/2406341/how-to-check-if-an-ip-address-is-the-local-host-on-a-multi-homed-system
  11. if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) { return true; }
  12. // Check if the address is defined on any interface
  13. try { return NetworkInterface.getByInetAddress(addr) != null; } catch (SocketException e) { return false;}
  14. }

URL: http://stackoverflow.com/questions/2406341/how-to-check-if-an-ip-address-is-the-local-host-on-a-multi-homed-system

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.