Skip to content

Instantly share code, notes, and snippets.

@kzar
Created Dec 7, 2011
Embed
What would you like to do?
Open and keep open a SSH tunnel using Clojure
(ns ssh-tunnel.core
(require [clj-ssh.ssh :as ssh]))
(defn ssh-tunnel
"Run a ssh tunnel in a deamon thread."
[session local-port remote-port]
(ssh/with-local-port-forward [session local-port remote-port]
(.setDaemonThread session true)
(ssh/with-connection session
(.setServerAliveInterval session 1000)
(while (ssh/connected? session)
(Thread/sleep 100)))))
(defn- tunnel [{:keys [host port user password]}]
(ssh/with-ssh-agent []
(loop []
(try
(let [port (Integer. port)
session (ssh/session host
:username user
:password password
:strict-host-key-checking :no)]
(ssh-tunnel session port port))
(catch RuntimeException e
(Thread/sleep 1000)))
(recur))))
(defn start-tunnel [settings]
(when settings
(doto (Thread. (partial tunnel settings))
(.setDaemon true)
(.start))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment