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
8
+
9
+ The ` clj-ssh.cli ` namespace provides some functions for ease of use at the REPL.
9
10
10
11
(use 'clj-ssh.ssh)
11
12
12
- There is a simple ` ssh ` function, which by default, will try and use a id_rsa
13
- key in your $HOME/. ssh directory .
13
+ There is a simple ` ssh ` function, which by default, will use the system
14
+ ssh-agent .
14
15
15
16
(ssh "hostname" "ls")
16
17
17
18
Strict host key checking can be turned off.
18
19
19
20
(default-session-options {:strict-host-key-checking :no})
20
21
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.
22
+ By default, your current username is used. If your key has a passphrase, and
23
+ you are on OSX, then you should be asked for access to your keychain. If you
24
+ are on any other OS without a ssh-agent , you will need to explicitly add your
25
+ key to the clj-ssh's ssh-agent with the appropriate add-identity call.
25
26
26
- More advance usage is possible.
27
+ SFTP is supported:
27
28
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))))))
29
+ ``` clj
30
+ (sftp " hostname" :put " /from/this/path" " to/this/path" )
31
+ ```
36
32
37
- SFTP is supported, both with a simple interface,
33
+ Note that any sftp commands that change the state of the sftp session (such as
34
+ cd) do not work with the simplified interface, as a new session is created each
35
+ time.
38
36
39
- (sftp "hostname" :put "/from/this/path" "to/this/path")
37
+ ### Non REPL
40
38
41
- as well as more advanced usage .
39
+ The ` clj-ssh.ssh ` namespace should be using SSH from functional code .
42
40
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"))))))
41
+ ``` clj
42
+ (let [agent (ssh-agent {:use-system-ssh-agent false })]
43
+ (add-identity agent " /user/name/.ssh/id_rsa" )
44
+ (let [session (session agent " localhost" {:strict-host-key-checking :no })]
45
+ (with-connection session
46
+ (let [result (ssh session {:in " echo hello" })]
47
+ (println (result :out )))
48
+ (let [result (ssh session " /bin/bash" " -c" " ls" " /" )]
49
+ (println (second result))))))
50
+ ```
50
51
51
- Note that any sftp commands that change the state of the sftp session (such as
52
- cd) do not work with the simplified interface, as a new session is created each
53
- time.
52
+ ``` clj
53
+ (let [agent (ssh-agent {})]
54
+ (let [session (session agent " localhost" {:strict-host-key-checking :no })]
55
+ (with-connection session
56
+ (let [channel (ssh-sftp session)]
57
+ (with-channel-connection channel
58
+ (sftp channel :cd " /remote/path" )
59
+ (sftp channel :put " /some/file" " filename" ))))))
60
+ ```
54
61
55
62
SSH tunneling is also supported:
56
63
57
64
``` clj
58
- (with- ssh-agent [ ]
59
- (let [session (session " localhost" :strict-host-key-checking :no )]
65
+ (let [agent ( ssh-agent { :use-system- ssh-agent false }) ]
66
+ (let [session (session agent " localhost" :strict-host-key-checking :no )]
60
67
(with-connection session
61
68
(with-local-port-forward [session 8080 80 ]
62
69
(comment do something with port 8080 here)))))
@@ -69,11 +76,9 @@ SSH tunneling is also supported:
69
76
70
77
## FAQ
71
78
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?
79
+ Q: What does
80
+ "4: Failure @ com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2289)"
81
+ during an sftp transfer signify?
77
82
78
83
A: Probably a disk full, or permission error.
79
84
@@ -82,7 +87,7 @@ A: Probably a disk full, or permission error.
82
87
Via [ clojars] ( http://clojars.org ) and
83
88
[ Leiningen] ( http://github.com/technomancy/leiningen ) .
84
89
85
- :dependencies [clj-ssh "0.3.3 "]
90
+ :dependencies [clj-ssh "0.4.0-SNAPSHOT "]
86
91
87
92
or your favourite maven repository aware tool.
88
93
0 commit comments