Skip to content

Instantly share code, notes, and snippets.

@carlohamalainen
Created December 18, 2012 20:45
Show Gist options
  • Save carlohamalainen/4331796 to your computer and use it in GitHub Desktop.
Save carlohamalainen/4331796 to your computer and use it in GitHub Desktop.
(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