Skip to content

Instantly share code, notes, and snippets.

@prestancedesign
Last active December 8, 2020 14:33
Show Gist options
  • Save prestancedesign/7d5b8b577932f17660cdaf5b902e409c to your computer and use it in GitHub Desktop.
Save prestancedesign/7d5b8b577932f17660cdaf5b902e409c to your computer and use it in GitHub Desktop.
DigitalOcean datacenter ping with Babashka (Clojure)
#!/usr/bin/env bb
(require '[babashka.curl :as curl]
'[clojure.java.shell :as shell]
'[clojure.string :as str])
(def url "http://speedtest-ams2.digitalocean.com/")
(def get-endpoints
(let [{:keys [body]} (curl/get url)]
(re-seq #"speedtest\-.+.digitalocean.com" body)))
(defn get-average [result]
(-> result
str/split-lines
last
(str/split #"/")
(get 4)))
(def mac? (str/starts-with? (System/getProperty "os.name") "Mac"));; TODO: test on Windows
(def timeout-arg (if mac? "-t3" "-w3"))
(defn ping-result [endpoint]
(let [{:keys [out]} (shell/sh "ping" "-c5" timeout-arg endpoint)
msg (str endpoint " => " (get-average out) "ms")]
(println msg)))
(doseq [endpoint get-endpoints]
(ping-result endpoint))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment