Created
April 16, 2010 07:22
-
-
Save apage43/368131 to your computer and use it in GitHub Desktop.
This file contains 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
(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