Skip to content

Instantly share code, notes, and snippets.

@babedev
Last active December 23, 2017 14:41
Show Gist options
  • Save babedev/7a5168235963457ead00045d62db3a32 to your computer and use it in GitHub Desktop.
Save babedev/7a5168235963457ead00045d62db3a32 to your computer and use it in GitHub Desktop.
Get IP address
static def getIPAddress() {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces()
while (interfaces.hasMoreElements()) {
Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses()
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement()
if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
return address.getHostAddress()
}
}
}
return InetAddress.getLocalHost().getHostAddress()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment