Skip to content

Commit 0740694

Browse files
committed
Add remote port forwarding support
1 parent 7ccb4bb commit 0740694

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/clj_ssh/ssh.clj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,29 @@ keys. All other option key pairs will be passed as SSH config options."
399399
(.toByteArray err-stream)
400400
(.toString err-stream out))]))))
401401

402+
(defn forward-remote-port
403+
"Start remote port forwarding"
404+
([session remote-port local-port local-host]
405+
(.setPortForwardingR session remote-port local-host local-port))
406+
([session remote-port local-port]
407+
(forward-remote-port session local-port remote-port "localhost")))
408+
409+
(defn unforward-remote-port
410+
"Remove remote port forwarding"
411+
[session remote-port]
412+
(.delPortForwardingR session remote-port))
413+
414+
(defmacro with-remote-port-forward
415+
"Creates a context in which a remote SSH tunnel is established for the session.
416+
(Use before the connection is opened.)"
417+
[[session remote-port local-port & [local-host & _]] & body]
418+
`(try
419+
(forward-remote-port
420+
~session ~remote-port ~local-port ~(or local-host "localhost"))
421+
~@body
422+
(finally
423+
(unforward-remote-port ~session ~remote-port))))
424+
402425
(defn forward-local-port
403426
"Start local port forwarding"
404427
([session local-port remote-port remote-host]

0 commit comments

Comments
 (0)