Skip to content

Instantly share code, notes, and snippets.

@davidsantiago
Created April 18, 2011 20:55
Show Gist options
  • Save davidsantiago/926154 to your computer and use it in GitHub Desktop.
Save davidsantiago/926154 to your computer and use it in GitHub Desktop.
SSH Port forwarding in Pallet!
(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