Last active
May 25, 2024 10:20
-
-
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/920e3b4df5364b8896c97ef1ba04b217b7f5d653
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 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.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