Skip to content

Commit 1d99321

Browse files
kzarhugoduncan
authored andcommitted
Added SSH tunneling.
1 parent dcd5791 commit 1d99321

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ Note that any sftp commands that change the state of the sftp session (such as
5252
cd) do not work with the simplified interface, as a new session is created each
5353
time.
5454

55+
SSH tunneling is also supported:
56+
57+
(with-ssh-agent []
58+
(let [session (session "localhost" :strict-host-key-checking :no)]
59+
(with-local-tunnel session 8080 80
60+
(with-connection session
61+
(while (connected? session)
62+
(Thread/sleep 100))))))
63+
64+
or more conveniently:
65+
66+
(with-ssh-agent []
67+
(let [session (session "localhost" :strict-host-key-checking :no)]
68+
(ssh-tunnel session 8080 80)))
69+
5570
## Documentation
5671

5772
[Annotated source](http:/hugoduncan.github.com/clj-ssh/uberdoc.html).

src/clj_ssh/ssh.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,25 @@ keys. All other option key pairs will be passed as SSH config options."
392392
(.toByteArray err-stream)
393393
(.toString err-stream out))]))))
394394

395+
(defmacro with-local-tunnel
396+
"Creates a context in which a local SSH tunnel is established for the session. (Use before the connection is opened.)"
397+
[session local-port remote-port & body]
398+
`(try
399+
(.setPortForwardingL ~session ~local-port "localhost" ~remote-port)
400+
~@body
401+
(finally
402+
(.delPortForwardingL ~session "localhost" ~local-port))))
403+
404+
(defn ssh-tunnel
405+
"Run a ssh tunnel."
406+
[session local-port remote-port]
407+
(with-local-tunnel session local-port remote-port
408+
(.setDaemonThread session true)
409+
(with-connection session
410+
(.setServerAliveInterval session 1000)
411+
(while (connected? session)
412+
(Thread/sleep 100)))))
413+
395414
(defn default-session [host username port password]
396415
(doto (session-impl
397416
(or (and (bound? #'*ssh-agent*) *ssh-agent*) (create-ssh-agent))

0 commit comments

Comments
 (0)