Created
June 28, 2011 21:37
-
-
Save davidsantiago/1052313 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defmacro with-ssh-tunnel | |
| "Execute the body with an ssh-tunnel available for the ports given in the | |
| tunnels map. tunnels should be a map from local ports (integers) to either | |
| 1) An integer remote port. Remote host is assumed to be 'localhost'. | |
| 2) A vector of remote host and remote port. eg, [\"yahoo.com\" 80]. | |
| Automatically closes the connection (and port forwards) on completion." | |
| [request tunnels & body] | |
| `(clj-ssh/with-ssh-agent [(execute/default-agent)] | |
| (let [user# (:user ~request) | |
| node-address# (compute/node-address (:target-node ~request)) | |
| node-ssh-port# (compute/ssh-port (:target-node ~request)) | |
| session# (clj-ssh/session node-address# | |
| :username (:username user#) | |
| :port node-ssh-port# | |
| :password (:password user#))] | |
| (clj-ssh/with-connection session# | |
| ;; Set up the port forwards | |
| (doseq [tunnel# ~tunnels] | |
| (let [lport# (first tunnel#) | |
| [rhost# rport#] (if (vector? (second tunnel#)) | |
| (second tunnel#) | |
| ["localhost" (second tunnel#)])] | |
| (.setPortForwardingL session# lport# rhost# rport#))) | |
| ~@body)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment