SSH in clojure. Uses jsch.
The clj-ssh.cli
namespace provides some functions for ease of use at the REPL.
(use 'clj-ssh.ssh)
There is a simple ssh
function, which by default, will use the system
ssh-agent.
(ssh "hostname" "ls")
Strict host key checking can be turned off.
(default-session-options {:strict-host-key-checking :no})
By default, your current username is used. If your key has a passphrase, and you are on OSX, then you should be asked for access to your keychain. If you are on any other OS without a ssh-agent, you will need to explicitly add your key to the clj-ssh's ssh-agent with the appropriate add-identity call.
SFTP is supported:
(sftp "hostname" :put "/from/this/path" "to/this/path")
Note that any sftp commands that change the state of the sftp session (such as cd) do not work with the simplified interface, as a new session is created each time.
The clj-ssh.ssh
namespace should be using SSH from functional code.
(let [agent (ssh-agent {:use-system-ssh-agent false})]
(add-identity agent "/user/name/.ssh/id_rsa")
(let [session (session agent "localhost" {:strict-host-key-checking :no})]
(with-connection session
(let [result (ssh session {:in "echo hello"})]
(println (result :out)))
(let [result (ssh session "/bin/bash" "-c" "ls" "/")]
(println (second result))))))
(let [agent (ssh-agent {})]
(let [session (session agent "localhost" {:strict-host-key-checking :no})]
(with-connection session
(let [channel (ssh-sftp session)]
(with-channel-connection channel
(sftp channel :cd "/remote/path")
(sftp channel :put "/some/file" "filename"))))))
SSH tunneling is also supported:
(let [agent (ssh-agent {:use-system-ssh-agent false})]
(let [session (session agent "localhost" :strict-host-key-checking :no)]
(with-connection session
(with-local-port-forward [session 8080 80]
(comment do something with port 8080 here)))))
Q: What does "4: Failure @ com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2289)" during an sftp transfer signify?
A: Probably a disk full, or permission error.
:dependencies [clj-ssh "0.4.0-SNAPSHOT"]
or your favourite maven repository aware tool.
Copyright © 2012 Hugo Duncan
Licensed under EPL