Last active
February 3, 2026 20:18
-
-
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
This file contains hidden or 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
| // 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