This executes a shell command in Scala and returns the actual server IP instead of 127.0.0.1 or the domain name of the host. #|
is used to simulate piping of commands. Process
and Seq
are used for awk
as $NF
will lead to compilation errors otherwise.
The actual shell command is: ip route get 8.8.8.8 | awk '{print $NF; exit}'
import scala.sys.process._ val addr = ("ip route get 8.8.8.8" #| Process(Seq("awk", "{print $NF; exit}"))).!!.trim val port = System.getProperty("http.port", "9000")
[UPDATE 08 APR 2017]
A friend pointed out that the shell command could have been replaced by hostname --all-ip-addresses
. This is preferred to hostname -i
as the latter may return 127.0.0.1 and is not recommended in the manual as it requires resolution of the hostname.