Created
September 12, 2016 07:04
-
-
Save dakatsuka/4c59943ab0ea350e1e7cb9b327f93daa to your computer and use it in GitHub Desktop.
Get private ipv4 address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.{Inet4Address, InetAddress, NetworkInterface} | |
import scala.collection.JavaConversions._ | |
def privateIPv4: Option[String] = NetworkInterface.getNetworkInterfaces | |
.filter(iface => iface.isUp && !iface.isLoopback && iface.getInetAddresses.hasMoreElements) | |
.foldLeft(List.empty[InetAddress])((acc, iface) => | |
iface.getInetAddresses.foldLeft(acc) { (addresses, address) => address :: addresses } | |
) | |
.filter(_.isInstanceOf[Inet4Address]) | |
.filter(_.isSiteLocalAddress) | |
.map(_.getHostName) | |
.headOption |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment