Created
April 18, 2011 20:55
-
-
Save davidsantiago/926154 to your computer and use it in GitHub Desktop.
SSH Port forwarding in Pallet!
This file contains 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)))) | |
(resource/deflocal ssh-test | |
(ssh-test* | |
[request] | |
(with-ssh-tunnel-> request {2222 5984 | |
2223 ["localhost" 8081]} | |
(execute/local-script ( curl -X GET "http://localhost:2222/_config") | |
(curl -X GET "http://localhost:2223/"))) | |
request)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment