Namespaces

 
0.2.0-SNAPSHOT permalink Arrow_down_16x16
  • (ssh session-or-hostname & args)
Execute commands over ssh.

ssh host-or-session cmd? & options

cmd specifies a command to exec. If no cmd is given, a shell is started and input is taken from :in.

Options are

:in specifies input to the remote shell. A string or a stream.
:out specify :bytes or a string with an encoding specification.
:return-map when followed by boolean true, sh returns a map of
:exit => sub-process's exit code
:out => sub-process's stdout (as byte[] or String)
:err => sub-process's stderr (as byte[] or String)
when not given or followed by false, ssh returns a vector
of the remote shell's stdout followed by its stderr
:username username to use for authentication
:password password to use for authentication
:port port to use if no session specified

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clj_ssh/ssh.clj:387 top

(defn ssh
  "Execute commands over ssh.

  ssh host-or-session cmd? & options

cmd specifies a command to exec.  If no cmd is given, a shell is started and input is taken from :in.

Options are

:in         specifies input to the remote shell. A string or a stream.
:out        specify :bytes or a string with an encoding specification.
:return-map when followed by boolean true, sh returns a map of
              :exit => sub-process's exit code
              :out  => sub-process's stdout (as byte[] or String)
              :err  => sub-process's stderr (as byte[] or String)
            when not given or followed by false, ssh returns a vector
            of the remote shell's stdout followed by its stderr
:username   username to use for authentication
:password   password to use for authentication
:port       port to use if no session specified
"
  [session-or-hostname & args]
  (let [opts (parse-args args)
        session-given (instance? com.jcraft.jsch.Session session-or-hostname)
        session (if session-given
                  session-or-hostname
                  (default-session
                   session-or-hostname
                   (opts :username)
                   (opts :port)
                   (opts :password)))]
    (try
     (when-not (connected? session)
       (connect session))
     (if (empty? (:cmd opts))
       (let [result (ssh-shell
                     session
                     (:in opts) (:out opts) (dissoc opts :in :out :cmd))]
         (if (opts :return-map)
           {:exit (first result) :out (second result)}
           result))
       (let [result (ssh-exec
                     session
                     (apply str (interpose " " (:cmd opts)))
                     (:in opts) (:out opts) (dissoc opts :in :out :cmd))]
         (if (opts :return-map)
           {:exit (first result) :out (second result) :err (last result)}
           result)))
     (finally
      (when-not session-given
        (disconnect session))))))
Vars in clj-ssh.ssh/ssh: defn empty? let when-not
Used in 0 other vars

Comments top

No comments for ssh. Log in to add a comment.