Skip to content

Instantly share code, notes, and snippets.

@apage43
Created April 16, 2010 07:22
Show Gist options
  • Save apage43/368131 to your computer and use it in GitHub Desktop.
Save apage43/368131 to your computer and use it in GitHub Desktop.
(import '(javax.swing JFrame JTextField JLabel JOptionPane JPanel JPasswordField)
'(java.awt Font Color Graphics2D))
(use 'clojure.contrib.io)
(defn exec [session cmd]
(let [channel (.openChannel session "exec")
reader (reader (.getInputStream channel))]
(.setCommand channel cmd)
(.connect channel)
(let [rv (doall (take-while #(not (nil? %)) (repeatedly #(.readLine reader))))]
(.disconnect channel)
rv)))
(defn authdialog []
(let [pass (ref "")]
(proxy [com.jcraft.jsch.UserInfo com.jcraft.jsch.UIKeyboardInteractive] []
(getPassword [] @pass)
(promptYesNo [_] true)
(getPassphrase [] nil)
(promptPassphrase [_] false)
(promptPassword [msg]
(let [pf (JPasswordField. 20)]
(if (= (JOptionPane/showConfirmDialog nil pf msg JOptionPane/OK_CANCEL_OPTION)
JOptionPane/OK_OPTION)
(dosync (ref-set pass (.getText pf)) true) false))))))
(defn ssh [host user]
"Connect and open a session"
(let [session (doto (.getSession (com.jcraft.jsch.JSch.) user host 22)
(.setUserInfo (authdialog))
(.setConfig "StrictHostKeyChecking" "no")
(.connect))]
session ))
(defn updater [ml ses cmd] (future
(Thread/sleep 1000)
(.setText ml (apply str (exec ses cmd)))
(if (.isConnected ses) (updater ml ses cmd))))
(defn watch-command [session cmd]
(let [mlabel (doto (JLabel.)
(.setFont (Font. "sansserif" Font/BOLD 32)))
frame (doto (JFrame.)
(.setSize 200 80)
(.add mlabel)
(.show))]
(updater mlabel session cmd)
frame))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment