1
1
# clj-ssh
2
2
3
- SSH in clojure. Uses jsch. Provides a ssh function that tries to look similar
4
- to clojure.contrib.shell/sh.
3
+ SSH in clojure. Uses jsch.
5
4
6
5
## Usage
7
6
8
- The top level namespace is ` clj-ssh.ssh `
7
+ ### REPL
9
8
10
- ( use 'clj-ssh.ssh)
9
+ The ` clj-ssh.cli ` namespace provides some functions for ease of use at the REPL.
11
10
12
- There is a simple ` ssh ` function, which by default, will try and use a id_rsa
13
- key in your $HOME/.ssh directory.
14
-
15
- (ssh "hostname" "ls")
16
-
17
- Strict host key checking can be turned off.
11
+ ``` clj
12
+ (use 'clj-ssh.cli)
13
+ ```
18
14
19
- (default-session-options {:strict- host-key-checking :no})
15
+ Use ` ssh ` to execute a command, say ` ls ` , on a remote host "my-host",
20
16
21
- By default, your current username and id_rsa key are used. If your key has a
22
- passphrase, and you are on OSX, then you should be asked for access to your
23
- keychain. If you are on any other OS, you will need to explicitly add your key
24
- to the clj-ssh's ssh-agent with the appropriate add-identity call.
17
+ ``` clj
18
+ ( ssh " my-host " " ls " )
19
+ => { :exit 0 :out " file1 \n file2 \n " :err " " )
20
+ ```
25
21
26
- More advance usage is possible.
22
+ By default this will use the system ssh-agent to obtain your ssh keys, and it
23
+ uses your current username, but this can be specified:
27
24
28
- (with-ssh-agent []
29
- (add-identity "/user/name/.ssh/id_dsa")
30
- (let [session (session "localhost" :strict-host-key-checking :no)]
31
- (with-connection session
32
- (let [result (ssh session :in "echo hello" :result-map true)]
33
- (println (result :out)))
34
- (let [result (ssh session "/bin/bash" "-c" "ls" "/")]
35
- (println (second result))))))
25
+ ```clj
26
+ (ssh " my-host" " ls" :username " remote-user" )
27
+ => {:exit 0 :out " file1\n file2\n " :err " " )
28
+ ```
36
29
37
- SFTP is supported, both with a simple interface,
30
+ Strict host key checking can be turned off:
38
31
39
- (sftp "hostname" :put "/from/this/path" "to/this/path")
32
+ ```clj
33
+ (default-session-options {:strict-host-key-checking :no })
34
+ ```
40
35
41
- as well as more advanced usage.
36
+ SFTP is also supported. For example, to copy a local file to a remote host
37
+ " my-host" :
42
38
43
- (with-ssh-agent []
44
- (let [session (session "localhost" :strict-host-key-checking :no)]
45
- (with-connection session
46
- (let [channel (ssh-sftp session)]
47
- (with-connection channel
48
- (sftp channel :cd "/remote/path")
49
- (sftp channel :put "/some/file" "filename"))))))
39
+ ```clj
40
+ (sftp " my-host" :put " /from/this/path" " to/this/path" )
41
+ ```
50
42
51
43
Note that any sftp commands that change the state of the sftp session (such as
52
44
cd) do not work with the simplified interface, as a new session is created each
53
45
time.
54
46
47
+ If your key has a passphrase, you will need to explicitly add your key either to
48
+ the system's ssh-agent, or to clj-ssh's ssh-agent with the appropriate
49
+ `add-identity` call.
50
+
51
+ ### Non REPL
52
+
53
+ The `clj-ssh.ssh` namespace should be using SSH from functional code.
54
+
55
+ ```clj
56
+ (let [agent (ssh-agent {:use-system-ssh-agent false })]
57
+ (add-identity agent " /user/name/.ssh/id_rsa" )
58
+ (let [session (session agent " localhost" {:strict-host-key-checking :no })]
59
+ (with-connection session
60
+ (let [result (ssh session {:in " echo hello" })]
61
+ (println (result :out )))
62
+ (let [result (ssh session {:cmd " ls" }]
63
+ (println (second result))))))
64
+ ```
65
+
66
+ The above example shows using `:in` to pass commands to a shell, and using
67
+ `:cmd` to exec a command without a shell. When using `:cmd` you can still pass
68
+ a stream or a string to `:in` to be used as the process' standard input.
69
+
70
+ ```clj
71
+ (let [agent (ssh-agent {})]
72
+ (let [session (session agent " localhost" {:strict-host-key-checking :no })]
73
+ (with-connection session
74
+ (let [channel (ssh-sftp session)]
75
+ (with-channel-connection channel
76
+ (sftp channel :cd " /remote/path" )
77
+ (sftp channel :put " /some/file" " filename" ))))))
78
+ ```
79
+
55
80
SSH tunneling is also supported:
56
81
57
82
```clj
58
- (with- ssh-agent [ ]
59
- (let [session (session " localhost" :strict-host-key-checking :no )]
83
+ (let [agent ( ssh-agent { :use-system- ssh-agent false }) ]
84
+ (let [session (session agent " localhost" :strict-host-key-checking :no )]
60
85
(with-connection session
61
86
(with-local-port-forward [session 8080 80 ]
62
87
(comment do something with port 8080 here)))))
@@ -69,11 +94,9 @@ SSH tunneling is also supported:
69
94
70
95
## FAQ
71
96
72
- Q: Why doesn't clj-ssh integrate with the OS's ssh agent?
73
-
74
- A: Java has no access to the Unix domain socket used by the system ssh-agent.
75
-
76
- Q: What does "4: Failure @ com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2289)" during an sftp transfer signify?
97
+ Q: What does
98
+ " 4: Failure @ com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2289)"
99
+ during an sftp transfer signify?
77
100
78
101
A: Probably a disk full, or permission error.
79
102
@@ -82,7 +105,7 @@ A: Probably a disk full, or permission error.
82
105
Via [clojars](http://clojars.org ) and
83
106
[Leiningen](http://github.com/technomancy/leiningen ).
84
107
85
- :dependencies [clj-ssh "0.3.3 "]
108
+ :dependencies [clj-ssh " 0.4.0 " ]
86
109
87
110
or your favourite maven repository aware tool.
88
111
0 commit comments