Skip to content

Commit 5415a8f

Browse files
committed
Merge branch 'release/0.3.1'
Conflicts: README.md ReleaseNotes.md pom.xml src/clj_ssh/reflect.clj
2 parents 7ba3512 + d117076 commit 5415a8f

File tree

8 files changed

+111
-27
lines changed

8 files changed

+111
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ target
44
lib
55
classes
66
autodoc/**
7+
doc/**

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,25 @@ Note that any sftp commands that change the state of the sftp session (such as
5252
cd) do not work with the simplified interface, as a new session is created each
5353
time.
5454

55+
SSH tunneling is also supported:
56+
57+
(with-ssh-agent []
58+
(let [session (session "localhost" :strict-host-key-checking :no)]
59+
(with-local-tunnel session 8080 80
60+
(with-connection session
61+
(while (connected? session)
62+
(Thread/sleep 100))))))
63+
64+
or more conveniently:
65+
66+
(with-ssh-agent []
67+
(let [session (session "localhost" :strict-host-key-checking :no)]
68+
(ssh-tunnel session 8080 80)))
69+
5570
## Documentation
5671

5772
[Annotated source](http:/hugoduncan.github.com/clj-ssh/uberdoc.html).
73+
[API](http:/hugoduncan.github.com/clj-ssh/api/0.3/index.html).
5874

5975
## FAQ
6076

@@ -71,7 +87,7 @@ A: Probably a disk full, or permission error.
7187
Via [clojars](http://clojars.org) and
7288
[Leiningen](http://github.com/technomancy/leiningen).
7389

74-
:dependencies [clj-ssh "0.3.0"]
90+
:dependencies [clj-ssh "0.3.1"]
7591

7692
or your favourite maven repository aware tool.
7793

ReleaseNotes.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Release Notes
22

3-
Current release is 0.3.0
3+
Current release is 0.3.1
4+
5+
## 0.3.1
6+
7+
- Allow clj-ssh to work with a wide range of slingshot versions
8+
Tested with slingshot 0.2.0 and 0.10.1
9+
10+
- Added SSH tunneling.
411

512
## 0.3.0
613

pom.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>clj-ssh</groupId>
55
<artifactId>clj-ssh</artifactId>
6-
<version>0.3.0</version>
6+
<version>0.3.1</version>
77
<name>clj-ssh</name>
88
<description>
99
clj-ssh is a clojure wrapper for he jsch library, and can be used as an
@@ -94,7 +94,7 @@
9494
<dependency>
9595
<groupId>slingshot</groupId>
9696
<artifactId>slingshot</artifactId>
97-
<version>0.2.0</version>
97+
<version>${slingshot.version}</version>
9898
</dependency>
9999
<dependency>
100100
<groupId>org.clojure</groupId>
@@ -141,13 +141,20 @@
141141
<profile>
142142
<id>clojure-1.3</id>
143143
<properties>
144-
<clojure.version>1.3.0-beta1</clojure.version>
144+
<clojure.version>1.3.0</clojure.version>
145+
</properties>
146+
</profile>
147+
<profile>
148+
<id>clojure-1.4</id>
149+
<properties>
150+
<clojure.version>1.4.0-alpha3</clojure.version>
145151
</properties>
146152
</profile>
147153
</profiles>
148154

149155
<properties>
150156
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
151157
<clojure.version>1.2.0</clojure.version>
158+
<slingshot.version>0.2.0</slingshot.version>
152159
</properties>
153160
</project>

project.clj

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
(defproject clj-ssh "0.3.0"
2-
:description "ssh from clojure"
1+
(defproject clj-ssh "0.3.1"
2+
:description "Library for using SSH from clojure."
33
:dependencies [[org.clojure/clojure "1.2.0"]
44
[org.clojure/tools.logging "0.1.2"]
55
[slingshot "0.2.0"]
66
[com.jcraft/jsch "0.1.44-1"]]
7-
:dev-dependencies [[swank-clojure "1.2.1"]
8-
[autodoc "0.7.1"]
9-
[log4j/log4j "1.2.14"]]
10-
:autodoc {:name "clj-ssh"
11-
:description "Library for using SSH from clojure."
12-
:copyright "Copyright Hugo Duncan 2010, 2011. All rights reserved."
13-
:web-src-dir "http://github.com/hugoduncan/clj-ssh/blob/"
14-
:web-home "http://hugoduncan.github.com/clj-ssh/" })
7+
:dev-dependencies [[log4j/log4j "1.2.14"]]
8+
:multi-deps {"slingshot-0.10.1" [[slingshot "0.10.1"]
9+
[org.clojure/clojure "1.2.1"]]
10+
"clojure-1.2.1" [[slingshot "0.10.1"]
11+
[org.clojure/clojure "1.2.1"]]
12+
"clojure-1.3.0" [[slingshot "0.10.1"]
13+
[org.clojure/clojure "1.3.0"]]
14+
"clojure-1.4.0" [[slingshot "0.10.1"]
15+
[org.clojure/clojure "1.4.0-beta1"]]}
16+
:codox {:writer codox-md.writer/write-docs
17+
:version "0.3"
18+
:output-dir "doc/api/0.3"})

src/clj_ssh/reflect.clj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
(.invoke obj (into-array Object args))))
2828

2929
(defn get-field
30-
"Access to private or protected field. field-name is a symbol or
31-
keyword."
30+
"Access to private or protected field. field-name is a symbol or
31+
keyword."
3232
[klass field-name obj]
33-
(-> klass (.getDeclaredField (name field-name))
34-
(doto (.setAccessible true))
35-
(.get obj)))
33+
(->
34+
klass
35+
(.getDeclaredField (name field-name))
36+
(doto (.setAccessible true))
37+
(.get obj)))

src/clj_ssh/ssh.clj

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ Licensed under EPL (http://www.eclipse.org/legal/epl-v10.html)"
4242
[clj-ssh.reflect :as reflect]
4343
[clojure.java.io :as io]
4444
[clojure.string :as string]
45-
[clojure.tools.logging :as logging]
46-
[slingshot.core :as slingshot])
45+
[clojure.tools.logging :as logging])
4746
(:import [com.jcraft.jsch
4847
JSch Session Channel ChannelShell ChannelExec ChannelSftp
4948
Identity IdentityFile Logger KeyPair]))
5049

50+
;; slingshot version compatability
51+
(try
52+
(use '[slingshot.slingshot :only [throw+]])
53+
(catch Exception _
54+
(use '[slingshot.core :only [throw+]])))
55+
5156
(def ^{:doc "SSH agent used to manage identities." :dynamic true}
5257
*ssh-agent*)
5358

@@ -392,6 +397,29 @@ keys. All other option key pairs will be passed as SSH config options."
392397
(.toByteArray err-stream)
393398
(.toString err-stream out))]))))
394399

400+
(defn forward-local-port
401+
"Start local port forwarding"
402+
([session local-port remote-port remote-host]
403+
(.setPortForwardingL session local-port remote-host remote-port))
404+
([session local-port remote-port]
405+
(forward-local-port session local-port remote-port "localhost")))
406+
407+
(defn unforward-local-port
408+
"Remove local port forwarding"
409+
[session local-port]
410+
(.delPortForwardingL session local-port))
411+
412+
(defmacro with-local-port-forward
413+
"Creates a context in which a local SSH tunnel is established for the session.
414+
(Use before the connection is opened.)"
415+
[[session local-port remote-port & [remote-host & _]] & body]
416+
`(try
417+
(forward-local-port
418+
~session ~local-port ~remote-port ~(or remote-host "localhost"))
419+
~@body
420+
(finally
421+
(unforward-local-port ~session ~local-port))))
422+
395423
(defn default-session [host username port password]
396424
(doto (session-impl
397425
(or (and (bound? #'*ssh-agent*) *ssh-agent*) (create-ssh-agent))
@@ -487,7 +515,7 @@ Options are
487515
5 (. target#
488516
(~name (first args#) (second args#) (nth args# 2) (nth args# 3)
489517
(nth args# 4)))
490-
(slingshot/throw+
518+
(throw+
491519
(java.lang.IllegalArgumentException.
492520
(str "Too many arguments passed. Limit 5, passed " (count args#)))))))
493521

@@ -533,7 +561,7 @@ Options are
533561
(conj args (sftp-modemap (options :mode)))
534562
args)]
535563
((memfn-varargs put) channel args))
536-
(slingshot/throw+
564+
(throw+
537565
(java.lang.IllegalArgumentException. (str "Unknown SFTP command " cmd)))))
538566

539567
(defn sftp
@@ -605,7 +633,7 @@ Options are
605633
[in]
606634
(let [code (.read in)]
607635
(when-not (zero? code)
608-
(slingshot/throw+
636+
(throw+
609637
{:type :clj-ssh/scp-failure
610638
:message (format
611639
"clj-ssh scp failure: %s"
@@ -685,7 +713,7 @@ Options are
685713
(fn [path]
686714
(let [file (java.io.File. path)]
687715
(when (.isDirectory file)
688-
(slingshot/throw+
716+
(throw+
689717
{:type :clj-ssh/scp-directory-copy-requested
690718
:message (format
691719
"Copy of dir %s requested without recursive flag"
@@ -838,7 +866,7 @@ Options are
838866
_ (when (and (.exists file)
839867
(not (.isDirectory file))
840868
(> (count remote-paths) 1))
841-
(slingshot/throw+
869+
(throw+
842870
{:type :clj-ssh/scp-copy-multiple-files-to-file-requested
843871
:message (format
844872
"Copy of multiple files to file %s requested"

test/clj_ssh/ssh_test.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,22 @@ list, Alan Dipert and MeikelBrandmeyer."
522522
(with-ssh-agent []
523523
(let [[priv pub] (generate-keypair :rsa 1024 "hello")]
524524
(add-identity *ssh-agent* "name" priv pub (.getBytes "hello")))))
525+
526+
(deftest forward-local-port-test
527+
(testing "minimal test"
528+
(with-ssh-agent [false]
529+
(add-identity (private-key-path))
530+
(let [session (session "localhost" :username (username)
531+
:strict-host-key-checking :no)]
532+
(is (instance? com.jcraft.jsch.Session session))
533+
(is (not (connected? session)))
534+
(connect session)
535+
(is (connected? session))
536+
(forward-local-port session 2222 22)
537+
(unforward-local-port session 2222)
538+
(forward-local-port session 2222 22 "localhost")
539+
(unforward-local-port session 2222)
540+
(with-local-port-forward [session 2222 22]
541+
(is true))
542+
(with-local-port-forward [session 2222 22 "localhost"]
543+
(is true))))))

0 commit comments

Comments
 (0)