@@ -37,21 +37,24 @@ Leiningen (http://github.com/technomancy/leiningen).
37
37
## License
38
38
39
39
Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
40
- (:use
41
- [clojure.contrib.def :only [defvar defvar- defunbound ]])
42
40
(:require
43
41
[clj-ssh.keychain :as keychain]
44
- [clojure.contrib.condition :as condition]
45
- [clojure.tools.logging :as logging]
46
- [clojure.contrib.reflect :as reflect]
42
+ [clj-ssh.reflect :as reflect]
47
43
[clojure.java.io :as io]
48
- [clojure.string :as string])
44
+ [clojure.string :as string]
45
+ [clojure.tools.logging :as logging]
46
+ [slingshot.core :as slingshot])
49
47
(:import [com.jcraft.jsch
50
48
JSch Session Channel ChannelShell ChannelExec ChannelSftp
51
49
Identity IdentityFile Logger KeyPair]))
52
50
53
- (defunbound *ssh-agent* " SSH agent used to manage identities." )
54
- (defvar *default-session-options* {} " Default SSH options" )
51
+ (def ^{:doc " SSH agent used to manage identities." :dynamic true }
52
+ *ssh-agent*)
53
+
54
+ (def
55
+ ^{:doc " Default SSH options"
56
+ :dynamic true }
57
+ *default-session-options* {})
55
58
56
59
(def ^{:dynamic true }
57
60
ssh-log-levels
@@ -97,7 +100,7 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
97
100
(defn- default-user []
98
101
(. System getProperty " user.name" ))
99
102
100
- (def ^String *default-identity*
103
+ (def ^{ :dynamic true } ^ String *default-identity*
101
104
(.getPath (io/file (. System getProperty " user.home" ) " .ssh" " id_rsa" )))
102
105
103
106
(defmacro with-default-identity
@@ -261,8 +264,13 @@ keys. All other option key pairs will be passed as SSH config options."
261
264
262
265
(defn disconnect
263
266
" Disconnect a session."
264
- ([session]
265
- (.disconnect session)))
267
+ [session]
268
+ (.disconnect session)
269
+ (when (instance? com.jcraft.jsch.Session session)
270
+ (when-let [t (reflect/get-field
271
+ com.jcraft.jsch.Session 'connectThread session)]
272
+ (when (.isAlive t)
273
+ (.interrupt t)))))
266
274
267
275
(defn connected?
268
276
" Predicate used to test for a connected session."
@@ -275,10 +283,11 @@ keys. All other option key pairs will be passed as SSH config options."
275
283
[session & body]
276
284
`(let [session# ~session]
277
285
(try
278
- (when-not (connected? session#)
279
- (connect session#))
280
- ~@body
281
- (finally (disconnect session#)))))
286
+ (when-not (connected? session#)
287
+ (connect session#))
288
+ ~@body
289
+ (finally
290
+ (disconnect session#)))))
282
291
283
292
(defn open-channel
284
293
" Open a channel of the specified type in the session."
@@ -478,10 +487,9 @@ Options are
478
487
5 (. target#
479
488
(~name (first args#) (second args#) (nth args# 2 ) (nth args# 3 )
480
489
(nth args# 4 )))
481
- (throw
490
+ (slingshot/ throw+
482
491
(java.lang.IllegalArgumentException.
483
- (str
484
- " too many arguments passed. Limit 5, passed " (count args#)))))))
492
+ (str " Too many arguments passed. Limit 5, passed " (count args#)))))))
485
493
486
494
(def sftp-modemap { :overwrite ChannelSftp/OVERWRITE
487
495
:resume ChannelSftp/RESUME
@@ -525,8 +533,8 @@ Options are
525
533
(conj args (sftp-modemap (options :mode )))
526
534
args)]
527
535
((memfn-varargs put) channel args))
528
- (throw ( java.lang.IllegalArgumentException.
529
- (str " Unknown SFTP command " cmd)))))
536
+ (slingshot/ throw+
537
+ ( java.lang.IllegalArgumentException. (str " Unknown SFTP command " cmd)))))
530
538
531
539
(defn sftp
532
540
" Execute SFTP commands.
@@ -597,15 +605,15 @@ Options are
597
605
[in]
598
606
(let [code (.read in)]
599
607
(when-not (zero? code)
600
- (condition/raise
601
- :type :clj-ssh/scp-failure
602
- :message (format
603
- " clj-ssh scp failure: %s"
604
- (case code
605
- 1 " scp error"
606
- 2 " scp fatal error"
607
- -1 " disconnect error"
608
- " unknown error" ))))))
608
+ (slingshot/throw+
609
+ { :type :clj-ssh/scp-failure
610
+ :message (format
611
+ " clj-ssh scp failure: %s"
612
+ (case code
613
+ 1 " scp error"
614
+ 2 " scp fatal error"
615
+ -1 " disconnect error"
616
+ " unknown error" ))} ))))
609
617
610
618
(defn- scp-send-command
611
619
" Send command to the specified output stream"
@@ -677,11 +685,11 @@ Options are
677
685
(fn [path]
678
686
(let [file (java.io.File. path)]
679
687
(when (.isDirectory file)
680
- (condition/raise
681
- :type :clj-ssh/scp-directory-copy-requested
682
- :message (format
683
- " Copy of dir %s requested without recursive flag"
684
- path)))
688
+ (slingshot/throw+
689
+ { :type :clj-ssh/scp-directory-copy-requested
690
+ :message (format
691
+ " Copy of dir %s requested without recursive flag"
692
+ path)} ))
685
693
file)))]
686
694
(map f paths)))
687
695
@@ -830,11 +838,11 @@ Options are
830
838
_ (when (and (.exists file)
831
839
(not (.isDirectory file))
832
840
(> (count remote-paths) 1 ))
833
- (condition/raise
834
- :type :clj-ssh/scp-copy-multiple-files-to-file-requested
835
- :message (format
836
- " Copy of multiple files to file %s requested"
837
- local-path)))
841
+ (slingshot/throw+
842
+ { :type :clj-ssh/scp-copy-multiple-files-to-file-requested
843
+ :message (format
844
+ " Copy of multiple files to file %s requested"
845
+ local-path)} ))
838
846
session-given (instance? com.jcraft.jsch.Session session-or-hostname)
839
847
session (if session-given
840
848
session-or-hostname
@@ -878,7 +886,7 @@ Options are
878
886
(when-not session-given
879
887
(disconnect session))))))
880
888
881
- (defvar- key-types {:rsa KeyPair/RSA :dsa KeyPair/DSA})
889
+ (def ^{ :private true } key-types {:rsa KeyPair/RSA :dsa KeyPair/DSA})
882
890
883
891
(defn generate-keypair
884
892
" Generate a keypair, returned as [private public] byte arrays.
0 commit comments