jsch_sftp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e51f25e22a07fb4bd8a98439c062fca66e7069cb
4
+ data.tar.gz: 96c0a5bd7e2198d6b1ee09cb25b192df596531b7
5
+ SHA512:
6
+ metadata.gz: 441822f3ddce6129d59022e70ad6a6a194bf9f3a8c1fd34a2b8f9a864b83687540725da435bf2927ffe0b393d908f95ccceafa1024234a4208ea35103603ffe9
7
+ data.tar.gz: 0cef60b9dccf5cacfab5c502f8eadd5b9722e90740a19bf13be4d5cd307e5c554855593dc9671861d0ab01064264b4719b2dc44dd515be48b76df4baf06d15ad
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'rspec'
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jsch_sftp (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (13.0.1)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.2)
16
+ rspec-support (~> 3.9.3)
17
+ rspec-expectations (3.9.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.3)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ jsch_sftp!
30
+ rake
31
+ rspec
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 AquisTech
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # jsch_sftp
2
+
3
+ A simple JRuby wrapper around the Java [JSch](http://www.jcraft.com/jsch/) library for SFTP implementation.
4
+ Current JSch version is `0.1.55`
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'jsch_sftp'
11
+
12
+ then execute:
13
+
14
+ $ bundle
15
+
16
+ ## Usage
17
+
18
+ ```
19
+ require 'jsch_sftp'
20
+ require 'jsch_sftp/sftp'
21
+
22
+ JschSFTP::SFTP.start('host', 'user', identity: '/path/to/keyfile') do |sftp|
23
+ # To list files/folders
24
+ sftp.entries('/').each do |entry|
25
+ puts entry.name
26
+ end
27
+
28
+ # To upload
29
+ sftp.upload!('/path/to/local', '/path/to/remote')
30
+
31
+ # To download
32
+ sftp.download!('/path/to/remote', '/path/to/local')
33
+ end
34
+ ```
35
+
36
+ ## Special Thanks
37
+ This library is a port of [jsch-sftp](https://github.com/modcloth-labs/jsch-sftp) but uses latest version of JSch.
38
+ Thanks madcloth-labs.
39
+ I have ported it with plans to upgrade and maintain it and also release it as a gem for ease of use.
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new('spec') do |t|
7
+ t.rspec_opts = "--tty --color"
8
+ end
9
+
10
+ task :default => :spec
11
+
12
+ load 'jsch_sftp.gemspec'
13
+
@@ -0,0 +1,30 @@
1
+ JSch 0.0.* was released under the GNU LGPL license. Later, we have switched
2
+ over to a BSD-style license.
3
+
4
+ ------------------------------------------------------------------------------
5
+ Copyright (c) 2002-2015 Atsuhiko Yamanaka, JCraft,Inc.
6
+ All rights reserved.
7
+
8
+ Redistribution and use in source and binary forms, with or without
9
+ modification, are permitted provided that the following conditions are met:
10
+
11
+ 1. Redistributions of source code must retain the above copyright notice,
12
+ this list of conditions and the following disclaimer.
13
+
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimer in
16
+ the documentation and/or other materials provided with the distribution.
17
+
18
+ 3. The names of the authors may not be used to endorse or promote products
19
+ derived from this software without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
22
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
24
+ INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
25
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27
+ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jsch_sftp/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "jsch_sftp"
8
+ gem.version = JschSFTP::VERSION
9
+ gem.authors = ['AquisTech']
10
+ gem.email = ['aquis.tech@gmail.com']
11
+ gem.description = %q{Jsch wrapper for SFTP in JRuby}
12
+ gem.summary = %q{Wraps Jsch library for easy implementation of SFTP in JRuby}
13
+ gem.homepage = 'https://github.com/AquisTech/jsch_sftp'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ end
@@ -0,0 +1,2 @@
1
+ require 'jsch_sftp/version'
2
+ require 'jsch_sftp/init'
@@ -0,0 +1,3 @@
1
+ require 'java'
2
+ $CLASSPATH << File.expand_path('../', __FILE__)
3
+ require 'jsch-0.1.55.jar'
@@ -0,0 +1,31 @@
1
+ require_relative 'sftp/session'
2
+
3
+ module JschSFTP
4
+ module SFTP
5
+
6
+ def self.start(host, user, options = {})
7
+ begin
8
+ session = connect(user, host, options)
9
+
10
+ yield session
11
+ rescue Exception => e
12
+ raise e
13
+ ensure
14
+ session.disconnect unless session.nil?
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def self.connect(user, host, options)
21
+ jsch = com.jcraft.jsch.JSch.new
22
+ configure(jsch, options)
23
+
24
+ Session.connect(jsch, user, host, options)
25
+ end
26
+
27
+ def self.configure(jsch, options)
28
+ jsch.add_identity(options[:identity]) if options.key?(:identity)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'entry'
2
+
3
+ module JschSFTP
4
+ module SFTP
5
+ class Channel
6
+
7
+ def initialize(channel)
8
+ @channel = channel
9
+ end
10
+
11
+ def connected?
12
+ channel.is_connected
13
+ end
14
+
15
+ def download!(remote_filepath, local_filepath)
16
+ channel.get(remote_filepath, local_filepath)
17
+ end
18
+
19
+ def upload!(local_filepath, remote_filepath)
20
+ channel.put(local_filepath, remote_filepath)
21
+ end
22
+
23
+ def mkdir(remote_path)
24
+ channel.mkdir(remote_path)
25
+ end
26
+
27
+ def entries(remote_path)
28
+ to_entries(channel.ls(remote_path))
29
+ end
30
+
31
+ def disconnect
32
+ if connected?
33
+ channel.disconnect rescue nil
34
+ end
35
+ end
36
+
37
+ def self.connect(session)
38
+ channel = session.open_channel("sftp")
39
+ channel.connect
40
+
41
+ self.new(channel)
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :channel
47
+
48
+ def to_entries(entries)
49
+ entries.map { |e| to_entry(e) }
50
+ end
51
+
52
+ def to_entry(entry)
53
+ Entry.new(entry.filename)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ module JschSFTP
2
+ module SFTP
3
+ class Entry
4
+
5
+ attr_reader :name
6
+
7
+ def initialize(name)
8
+ @name = name
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,60 @@
1
+ require_relative 'channel'
2
+
3
+ module JschSFTP
4
+ module SFTP
5
+ class Session
6
+ attr_reader :channel
7
+
8
+ def initialize(session, channel)
9
+ @session, @channel = session, channel
10
+ end
11
+
12
+ def connected?
13
+ session.is_connected
14
+ end
15
+
16
+ def download!(remote_filepath, local_filepath)
17
+ channel.download!(remote_filepath, local_filepath)
18
+ end
19
+
20
+ def entries(remote_path)
21
+ channel.entries(remote_path)
22
+ end
23
+
24
+ def upload!(local_filepath, remote_filepath)
25
+ channel.upload!(local_filepath, remote_filepath)
26
+ end
27
+
28
+ def mkdir(remote_path)
29
+ channel.mkdir(remote_path)
30
+ end
31
+
32
+ def disconnect
33
+ channel.disconnect unless channel.nil?
34
+
35
+ if connected?
36
+ session.disconnect rescue nil
37
+ end
38
+ end
39
+
40
+ def self.connect(jsch, user, host, options)
41
+ session = jsch.get_session(user, host)
42
+ configure(session, options)
43
+ session.connect
44
+
45
+ channel = Channel.connect(session)
46
+
47
+ self.new(session, channel)
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :session
53
+
54
+ def self.configure(session, options)
55
+ session.set_password(options[:password]) if options.key?(:password)
56
+ session.set_config("StrictHostKeyChecking", "no")
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module JschSFTP
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,71 @@
1
+ require_relative '../../../lib/jsch_sftp/init'
2
+ require_relative '../../../lib/jsch_sftp/sftp'
3
+ require 'fileutils'
4
+ require 'tmpdir'
5
+
6
+ describe JschSFTP::SFTP do
7
+ let(:host) { 'localhost' }
8
+ let(:user) { ENV['USER'] }
9
+
10
+ before(:all) do
11
+ @sourcedir = File.join(File.expand_path(File.dirname(__FILE__)), '../../')
12
+ @destdir = Dir.mktmpdir('sftp_work')
13
+
14
+ home = Java::JavaLang::System.get_property('user.home')
15
+ @identity = File.join(home, '.ssh/id_rsa')
16
+ end
17
+
18
+ context "downloading a file" do
19
+ it "should download the given remote file to the given local path" do
20
+ remote_file = File.join(@sourcedir, 'test.tgz')
21
+ local_file = File.join(@destdir, 'test.tgz.down')
22
+
23
+ described_class.start(host, user, identity: @identity) do |sftp|
24
+ sftp.download!(remote_file, local_file)
25
+ end
26
+
27
+ File.exists?(local_file).should be_true
28
+ end
29
+ end
30
+
31
+ context "uploading a file" do
32
+ it "should upload the given local file to the given remote path" do
33
+ local_file = File.join(@sourcedir, 'test.tgz')
34
+ remote_file = File.join(@destdir, 'test.tgz.up')
35
+
36
+ described_class.start(host, user, identity: @identity) do |sftp|
37
+ sftp.upload!(local_file, remote_file)
38
+ end
39
+
40
+ File.exists?(remote_file).should be_true
41
+ end
42
+ end
43
+
44
+ context "listing the contents of a remote directory" do
45
+ it "should return a list of the entries in the remote directory" do
46
+ FileUtils.touch(File.join(@destdir, 'empty1.txt'))
47
+ FileUtils.touch(File.join(@destdir, 'empty2.txt'))
48
+ names = []
49
+
50
+ described_class.start(host, user, identity: @identity) do |sftp|
51
+ sftp.entries(@destdir).each { |f| names << f.name }
52
+ end
53
+
54
+ names.should be_include('empty1.txt')
55
+ names.should be_include('empty2.txt')
56
+ end
57
+ end
58
+
59
+ context "making a remote directory" do
60
+ it "should create the specified directory on the remote server" do
61
+ remote_dir = File.join(@destdir, 'test_dir')
62
+
63
+ described_class.start(host, user, identity: @identity) do |sftp|
64
+ sftp.mkdir(remote_dir)
65
+ end
66
+
67
+ File.exists?(remote_dir).should be_true
68
+ File.directory?(remote_dir).should be_true
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,44 @@
1
+ require_relative '../../../lib/jsch_sftp/init'
2
+ require_relative '../../../lib/jsch_sftp/sftp'
3
+
4
+ describe JschSFTP::SFTP do
5
+
6
+ context "opening a remote connection" do
7
+ let(:sftp) { double('sftp', disconnect: nil) }
8
+
9
+ before(:each) do
10
+ described_class.stub(:connect).and_return(sftp)
11
+ end
12
+
13
+ context "when the commands complete successfully" do
14
+ it "should automatically close the connection" do
15
+ sftp.should_receive(:disconnect)
16
+
17
+ described_class.start('host', 'user') do |sftp|
18
+ # noop
19
+ end
20
+ end
21
+ end
22
+
23
+ context "when an error occurs while executing one of the commands" do
24
+ it "should automatically close the connection" do
25
+ sftp.should_receive(:disconnect)
26
+
27
+ begin
28
+ described_class.start('host', 'user') do |sftp|
29
+ raise StandardError
30
+ end
31
+ rescue
32
+ end
33
+ end
34
+
35
+ it "should re-raise the error" do
36
+ expect {
37
+ described_class.start('host', 'user') do |sftp|
38
+ raise StandardError
39
+ end
40
+ }.to raise_error
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'jsch_sftp'
3
+
4
+ Rspec.configure do |config|
5
+
6
+ end
Binary file
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsch_sftp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AquisTech
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Jsch wrapper for SFTP in JRuby
28
+ email:
29
+ - aquis.tech@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - jsch-LICENSE.txt
41
+ - jsch_sftp.gemspec
42
+ - lib/jsch_sftp.rb
43
+ - lib/jsch_sftp/init.rb
44
+ - lib/jsch_sftp/jsch-0.1.55.jar
45
+ - lib/jsch_sftp/sftp.rb
46
+ - lib/jsch_sftp/sftp/channel.rb
47
+ - lib/jsch_sftp/sftp/entry.rb
48
+ - lib/jsch_sftp/sftp/session.rb
49
+ - lib/jsch_sftp/version.rb
50
+ - spec/lib/jsch_sftp/end_to_end_spec.rb
51
+ - spec/lib/jsch_sftp/sftp_spec.rb
52
+ - spec/spec_helper.rb
53
+ - spec/test.tgz
54
+ homepage: https://github.com/AquisTech/jsch_sftp
55
+ licenses: []
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.6.11
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Wraps Jsch library for easy implementation of SFTP in JRuby
77
+ test_files:
78
+ - spec/lib/jsch_sftp/end_to_end_spec.rb
79
+ - spec/lib/jsch_sftp/sftp_spec.rb
80
+ - spec/spec_helper.rb
81
+ - spec/test.tgz