Created
February 6, 2018 18:28
-
-
Save cgrand/1122cea2c8b460365c6484f322455e64 to your computer and use it in GitHub Desktop.
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
(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