oxidized-ssh 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/oxidized/ssh.rb +122 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c4ade74682ead292057bf3f00d6c95ba4fb231b9
|
4
|
+
data.tar.gz: bcab6ffc07e1135ac3c6728cb09ff13b7bf09ebd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90cf2a053dcb3a64536785497bc51c90c286b11e328e809c7de35aef6953ca5ff49fd58ba8d00afa33b3662aa1716b0090dfd2afbfa5105342d4210334e22f7f
|
7
|
+
data.tar.gz: 4ac456bb4316dbac59d5dfff8dfb763c06c1ae2c3039c4eff6a566e73d32b77b7dd9a1c45b3c3e88208b6250936033699cc753319bffa61a14191fd9f81abc15
|
data/lib/oxidized/ssh.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require_relative "ssh/version"
|
2
|
+
require 'net/ssh'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
module Oxidized
|
6
|
+
class Ssh
|
7
|
+
|
8
|
+
attr_reader :connection, :ip, :username, :password
|
9
|
+
attr_reader :prompt, :verbosity, :exec, :pty_options
|
10
|
+
attr_reader :port, :output, :session
|
11
|
+
|
12
|
+
def initialize(options)
|
13
|
+
@ip = options[:ip]
|
14
|
+
@username = options[:username]
|
15
|
+
@password = options[:password]
|
16
|
+
@verbosity = options[:verbosity]
|
17
|
+
@prompt = options[:prompt]
|
18
|
+
@exec = options[:exec]
|
19
|
+
@pty_options = options[:pty_options] ||= { term: "vt100" }
|
20
|
+
@port = options[:port] ||= 22
|
21
|
+
@output = String.new
|
22
|
+
@logger = options[:logger] ||= Logger.new(STDOUT)
|
23
|
+
end
|
24
|
+
|
25
|
+
def start
|
26
|
+
raise "MissingSSHLibrary" if !defined? Net::SSH
|
27
|
+
@connection = Net::SSH.start(@ip, @username, password: @password, verbose: @verbosity, port: @port)
|
28
|
+
return yield self if block_given?
|
29
|
+
return (@connection and not @connection.closed?)
|
30
|
+
end
|
31
|
+
|
32
|
+
def exec!(params)
|
33
|
+
check_for_connection
|
34
|
+
exec(params)
|
35
|
+
@output.gsub(/\r\n/,/\n/)
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_for_connection
|
39
|
+
prep_connection unless @session
|
40
|
+
end
|
41
|
+
|
42
|
+
def exec(params)
|
43
|
+
@logger.debug "sending command #{params} with expectation of #{@prompt}"
|
44
|
+
if @exec
|
45
|
+
@connection.exec!(params)
|
46
|
+
else
|
47
|
+
collect_output(params)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def collect_output(params)
|
52
|
+
send_data(params + "\n")
|
53
|
+
return @output
|
54
|
+
end
|
55
|
+
|
56
|
+
def send_data(params)
|
57
|
+
expect @prompt
|
58
|
+
reset_output_buffer
|
59
|
+
@session.send_data params
|
60
|
+
@session.process
|
61
|
+
expect @prompt
|
62
|
+
end
|
63
|
+
|
64
|
+
def expect *regexps
|
65
|
+
regexps = [regexps].flatten
|
66
|
+
@logger.debug "expecting #{regexps.inspect} at #{@ip}"
|
67
|
+
@connection.loop(0.1) do
|
68
|
+
sleep 0.1
|
69
|
+
match = regexps.find { |regexp| @output.match regexp }
|
70
|
+
return match if match
|
71
|
+
true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def prep_connection
|
76
|
+
return true if @exec
|
77
|
+
start_channel_requests
|
78
|
+
end
|
79
|
+
|
80
|
+
def start_channel_requests
|
81
|
+
create_session
|
82
|
+
end
|
83
|
+
|
84
|
+
def create_session
|
85
|
+
@session = @connection.open_channel do |channel|
|
86
|
+
setup_channels(channel)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def setup_channels(ch)
|
91
|
+
set_data_hook(ch)
|
92
|
+
request_channels(ch)
|
93
|
+
end
|
94
|
+
|
95
|
+
def set_data_hook(ch)
|
96
|
+
ch.on_data do |_ch, data|
|
97
|
+
@output << data
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def request_channels(ch)
|
102
|
+
ch.request_pty @pty_options do |_ch, success_pty|
|
103
|
+
raise NoShell, "Can't get PTY" unless success_pty
|
104
|
+
ch.send_channel_request 'shell' do |_ch, success_shell|
|
105
|
+
raise NoShell, "Can't get shell" unless success_shell
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def reset_output_buffer
|
111
|
+
@output = ''
|
112
|
+
end
|
113
|
+
|
114
|
+
def disconnect
|
115
|
+
Timeout::timeout(5) { @connection.loop }
|
116
|
+
rescue Errno::ECONNRESET, Net::SSH::Disconnect, IOError
|
117
|
+
ensure
|
118
|
+
(@connection.close rescue true) unless @connection.closed?
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oxidized-ssh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Schylar
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-ssh
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.8'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.8'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mocha
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.1'
|
97
|
+
description: SSH client that supports shell and exec channels
|
98
|
+
email:
|
99
|
+
- sutley@cwep.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- lib/oxidized/ssh.rb
|
105
|
+
homepage: http://github.com/MajesticFalcon/oxidized-ssh
|
106
|
+
licenses:
|
107
|
+
- Apache-2.0
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 2.0.0
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project: oxidized-ssh
|
125
|
+
rubygems_version: 2.6.7
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Robust SSH client
|
129
|
+
test_files: []
|