How to use the PipedInputStream to stream to stdout? #19

Open
jeroenvandijk opened this Issue Feb 19, 2013 · 2 comments

Comments

Projects
None yet
3 participants

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?

abailly commented Dec 16, 2013

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.

mattford63 commented Jan 25, 2017 edited

I did the above successfully with the following final line

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment