Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:18
Show Gist options
  • Select an option

  • Save dacr/9375513c5b3673aa9671b01473fec80d to your computer and use it in GitHub Desktop.

Select an option

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/86710214b501625b686fd85caa946a13d4dae6f0
// summary : get my local IPs and my remote IPs
// keywords : scala, dns, resolution, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// 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.4.2"
// ---------------------
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