Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created February 6, 2018 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/1122cea2c8b460365c6484f322455e64 to your computer and use it in GitHub Desktop.
Save cgrand/1122cea2c8b460365c6484f322455e64 to your computer and use it in GitHub Desktop.
(ns ssh-repl.client
(:require [clojure.java.io :as io]))
(defn connect
([user host repl-port {:as options
:keys [ssh-port repl-host key password]
:or {ssh-port 22
repl-host "localhost"}}]
(let [client (doto (org.apache.sshd.client.SshClient/setUpDefaultClient) .start)
session (-> client (.connect user host ssh-port) (doto .await) .getSession)]
(if key
(.addPublicKeyIdentity session
(org.apache.sshd.common.util.security.SecurityUtils/loadKeyPairIdentity
"shhhh" (io/input-stream key)
(reify org.apache.sshd.common.config.keys.FilePasswordProvider
(getPassword [_ _] password))))
(.addPasswordIdentity session password))
(-> session .auth .verify)
{:ssh-session session
:create-pair
(fn []
(let [channel (.createDirectTcpipChannel session
(org.apache.sshd.common.util.net.SshdSocketAddress. "ssh-repl-client" 0)
(org.apache.sshd.common.util.net.SshdSocketAddress. repl-host repl-port))]
(-> channel .open .verify)
{:in (-> channel .getInvertedIn (io/writer :encoding "UTF-8"))
:out (-> channel .getInvertedOut (io/reader :encoding "UTF-8"))}))})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment