1

I use a dynamic var to make an easy way to use ssh. However, it suddenly stops working in a multi-parameter for!

So, here is my core.clj (which is kind of sketchy now):

(use 'clj-ssh.ssh)

(def the-agent (ssh-agent {}))

(def ^:dynamic *session* nil)

(defmacro on-host [host & body]
  `(binding [*session* (clj-ssh.ssh/session the-agent ~host {})]
    ~@body))

(defn cmd [& args]
  (split (:out (ssh *session* {:cmd (join " " args)})) #"\n"))

(defn attempt-1 []
  (cmd "ls -a"))
(defn attempt-2 []
  (for [f (cmd "ls -a")]
    f))
(defn attempt-3 []
  (for [r (range 3)
        f (cmd "ls -a")]
    [r f]))

For some reason, first two trial functions work, and the third doesn't (the hosts and files are censured):

user=> (on-host "(some host)" (attempt-1))
["." ".." ".ackrc" ...]

user=> (on-host "(some host)" (attempt-2))
("." ".." ".ackrc" ...)

user=> (on-host "(some host)" (attempt-3))

IllegalArgumentException No implementation of method: :connected? of protocol: #'clj-ssh.ssh.protocols/Session found for class: nil  clojure.core/-cache-protocol-fn (core_deftype.clj:544)

Just in case you need a stacktrace:

user=> (use 'clojure.stacktrace)    
nil
user=>  (print-stack-trace *e 7)
java.lang.IllegalArgumentException: No implementation of method: :connected? of protocol: #'clj-ssh.ssh.protocols/Session found for class: nil
 at clojure.core$_cache_protocol_fn.invoke (core_deftype.clj:544)
    clj_ssh.ssh.protocols$eval1554$fn__1566$G__1543__1571.invoke (protocols.clj:4)
    clj_ssh.ssh$connected_QMARK_.invoke (ssh.clj:411)
    clj_ssh.ssh$ssh.invoke (ssh.clj:712)
    census.core$cmd.doInvoke (core.clj:15)
    clojure.lang.RestFn.invoke (RestFn.java:408)
    census.core$attempt_3$iter__1949__1955$fn__1956.invoke (core.clj:29)
nil

I'm really not sure what it is all about. Can you help me? Thank you!

1 Answer 1

2

Use doseq, not for

What you have there is a lazy sequence that will be evaluated after the binding form has returned. doseq forces evaluation.

For further reading:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.