Last active
November 22, 2023 14:09
-
-
Save nikolavojicic/66518c85a9b0a4d2e2c8605475f0d50b to your computer and use it in GitHub Desktop.
Get IP address and PID in Clojure
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
(defn ip [] | |
(try | |
(-> (InetAddress/getLocalHost) (.getHostAddress)) | |
(catch Throwable __ | |
(->> (NetworkInterface/getNetworkInterfaces) | |
(enumeration-seq) | |
(xf/some | |
(comp (keep (fn [^NetworkInterface network] | |
(when (and (.isUp network) | |
(not (.isLoopback network))) | |
(.getInetAddresses network)))) | |
(mapcat enumeration-seq) | |
(keep (fn [^InetAddress address] | |
(when-not (or (.isAnyLocalAddress address) | |
(.isLoopbackAddress address) | |
(.isMulticastAddress address)) | |
(.getHostAddress address)))) | |
(filter #(str/includes? % ".")))))))) | |
(defn pid [] | |
(-> (ProcessHandle/current) (.pid))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment