Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns tryssh.core
- (:use clojure.repl)
- (:import [ch.ethz.ssh2 Connection HTTPProxyData Session StreamGobbler]))
- ;; Sample proxied ssh client with ganymed2 java library.
- ;; Put this in your lein dependencies: [ch.ethz.ganymed/ganymed-ssh2 "261"]
- ;; Compare to https://code.google.com/p/ganymed-ssh-2/source/browse/trunk/examples/BasicWithHTTPProxy.java
- (def user-key "/home/myuser/mykey")
- (def target-user "myuser")
- (def target-host "12.34.56.78")
- (def proxy-host "myproxy.myhost.com")
- (def proxy-port 8080)
- (defn ssh [command]
- (with-open [connection (Connection. target-host)]
- (doto connection
- (.setProxyData (HTTPProxyData. proxy-host proxy-port))
- (.connect))
- (if-not (.authenticateWithPublicKey connection
- target-user (java.io.File. user-key) nil)
- (throw (Exception. (str "Unable to authenticate as user " target-user
- " with key file " key))))
- (with-open [session (.openSession connection)]
- (.execCommand session command)
- (with-open [err (StreamGobbler. (.getStderr session))]
- (with-open [out (StreamGobbler. (.getStdout session))]
- (println "Stdout:")
- (println (slurp out))
- (println "Stderr:")
- (println (slurp err))))
- (println "rc=" (.getExitStatus session)))))
- (ssh "ls -la")
Advertisement
Add Comment
Please, Sign In to add comment