Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/9375513c5b3673aa9671b01473fec80d to your computer and use it in GitHub Desktop.
Save dacr/9375513c5b3673aa9671b01473fec80d to your computer and use it in GitHub Desktop.
get my local IPs and my remote IPs / published by https://github.com/dacr/code-examples-manager #20e89a12-9183-4d0b-9c82-1f55bc583cd0/c9e32ccf6035e3cd98b69a7705568581cc12ca41
// summary : get my local IPs and my remote IPs
// keywords : scala, dns, resolution, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 20e89a12-9183-4d0b-9c82-1f55bc583cd0
// created-on : 2020-07-18T15:24:11Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.1.1"
// ---------------------
def hostname(): String = java.net.InetAddress.getLocalHost().getHostName
def hostAddress(): String = java.net.InetAddress.getLocalHost().getHostAddress
def lanAddresses(): List[String] = {
import scala.jdk.CollectionConverters._
java.net.NetworkInterface
.getNetworkInterfaces().asScala
.filterNot(_.isLoopback)
.filterNot(_.isVirtual)
.filter(_.isUp)
.toList
.flatMap { interface =>
val ips = interface
.getInetAddresses.asScala
.to(List)
.filterNot(_.isAnyLocalAddress)
.collect { case x: java.net.Inet4Address => x.getHostAddress }
ips.headOption
}
}
println("hostname = " + hostname())
println("address = " + hostAddress())
println("addresses= "+ lanAddresses().mkString(", "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment