Created
December 18, 2012 20:45
-
-
Save carlohamalainen/4331796 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
(defn create-session [hostname username password] | |
(doto (. (new JSch) getSession username hostname 22) | |
(.setPassword password) | |
(.setConfig "StrictHostKeyChecking" "no") | |
(.connect) | |
(.setServerAliveInterval 1000) | |
(.setServerAliveCountMax 30))) | |
(defn sleep [seconds] | |
(. Thread (sleep (* 1000 seconds)))) | |
(defn sendcommand [session command show-command?] | |
(let [channel (. session (openChannel "exec")) | |
stdout-buf (new BufferedReader (new InputStreamReader (. channel (getInputStream)) "UTF-8")) | |
stderr-buf (new BufferedReader (new InputStreamReader (. channel (getErrStream)) "UTF-8"))] | |
(when show-command? (println (str "sendcommand executing: " command))) | |
(. channel (setCommand command)) | |
(. channel (setInputStream nil)) | |
(. channel (connect)) | |
(while (not (. channel (isClosed))) | |
(sleep 1)) | |
(. channel (disconnect)) | |
{:exit-status (. channel (getExitStatus)) | |
:stdout (apply str (line-seq stdout-buf)) | |
:stderr (apply str (line-seq stderr-buf))})) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment