Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created March 21, 2010 11:28
Show Gist options
  • Save pervognsen/339248 to your computer and use it in GitHub Desktop.
Save pervognsen/339248 to your computer and use it in GitHub Desktop.
(ns port-scanner
(:import [java.net Socket InetSocketAddress]))
(defn port-open? [hostname port timeout]
(try
(with-open [sock (Socket.)]
(.connect sock (InetSocketAddress. hostname port) timeout)
port)
(catch Exception e false)))
(let [hostname (or (first *command-line-args*)
(do (println "Usage: scanner <hostname>")
(System/exit 1)))]
(println (str "Scanning " hostname "..."))
(doseq [port (doall (map #(future (port-open? hostname % 5000)) (range 1 1024)))]
(when @port (println @port "is open"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment