Skip to content

Instantly share code, notes, and snippets.

@nikolavojicic
Last active November 22, 2023 14:09
Show Gist options
  • Save nikolavojicic/66518c85a9b0a4d2e2c8605475f0d50b to your computer and use it in GitHub Desktop.
Save nikolavojicic/66518c85a9b0a4d2e2c8605475f0d50b to your computer and use it in GitHub Desktop.
Get IP address and PID in Clojure
(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