clj-commons/clj-ssh

How to use the PipedInputStream to stream to stdout?

jeroenvandijk opened this issue · 2 comments

Sorry to bother you with a "support" question. Normal usage of this library works really great, thanks for that.

Now, I would like to stream ssh session output to stdout. I tried several variations of the below and I also tried examples I found on the web, but without luck.

(require '[clj-ssh.ssh :as s])
(require '[clj-ssh.cli :refer [ssh]])

(let [{shell :channel out-stream :out-stream} (ssh host :cmd "sleep 10; echo hi" :strict-host-key-checking :no :out :stream)]
(s/with-channel-connection shell (while (s/connected-channel? shell) (println "connected")) (println "nothing")))

Am I missing something?

I have tried the following but it does not work: ssh exits after first line of input gets retrieved.

(defn stream-events
  "Connect to gerrit server and stream events to stdout"
  [host port]
  (let [agent (ssh-agent {:use-system-ssh-agent false})]
    (add-identity agent {:private-key-path "/home/user/.ssh/id_rsa"})
    (let [session (session agent host {:strict-host-key-checking :no
                                                       :port port})]
      (with-connection session
        (let [result (ssh session {:cmd "gerrit stream-events"
                                   :out :stream})
              reader (io/reader (:out-stream result))]
          (map #'println (line-seq reader)))))))

I also would like to be able to stream the output from the remote shell, here to simply propagate to stdout but later on to do more interesting things.

I did the above successfully with the following final line

(doall (map println (line-seq reader))))))))