spectre-ssh 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/spectre/ssh.rb +149 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ba7f13a54fa1b81b4a1421363753173b28e1d270c9285a33e1ecb08047739e21
|
4
|
+
data.tar.gz: a927c8e8685d729de0f3aeb663411e1bafe7f32371ec8d8fc34139495bae1f5f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 04a3a857cbccfc082535fd0e8342bcd269a978227652b0be4818a134793538bd52f491d16503f560b0f9e06aae3615a26dc4790f67a995c82091b28290368718
|
7
|
+
data.tar.gz: cf1340384a2d2da1bf70def699e85ab5c10f21ced75f75d766a044160c9dfb3666a700c9d92e0ee3bccaaef45ad5000ec02ca0dea7368befdbf6d20dc41a1a12
|
data/lib/spectre/ssh.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
|
5
|
+
module Spectre
|
6
|
+
module SSH
|
7
|
+
@@cfg = {}
|
8
|
+
|
9
|
+
class SSHConnection < DslClass
|
10
|
+
def initialize host, username, opts, logger
|
11
|
+
opts[:non_interactive] = true
|
12
|
+
|
13
|
+
@__logger = logger
|
14
|
+
@__host = host
|
15
|
+
@__username = username
|
16
|
+
@__opts = opts
|
17
|
+
@__session = nil
|
18
|
+
@__exit_code = nil
|
19
|
+
@__output = ''
|
20
|
+
end
|
21
|
+
|
22
|
+
def file_exists path
|
23
|
+
exec "ls #{path}"
|
24
|
+
exit_code == 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def owner_of path
|
28
|
+
exec "stat -c %U #{path}"
|
29
|
+
output.chomp
|
30
|
+
end
|
31
|
+
|
32
|
+
def connect!
|
33
|
+
return unless @__session == nil or @__session.closed?
|
34
|
+
@__session = Net::SSH.start(@__host, @__username, @__opts)
|
35
|
+
end
|
36
|
+
|
37
|
+
def close
|
38
|
+
return unless @__session and not @__session.closed?
|
39
|
+
@__session.close
|
40
|
+
end
|
41
|
+
|
42
|
+
def can_connect?
|
43
|
+
@__output = nil
|
44
|
+
|
45
|
+
begin
|
46
|
+
connect!
|
47
|
+
@__session.open_channel.close
|
48
|
+
@__output = "successfully connected to #{@__host} with user #{@__username}"
|
49
|
+
@__exit_code = 0
|
50
|
+
return true
|
51
|
+
rescue Exception => e
|
52
|
+
@__logger.error e.message
|
53
|
+
@__output = "unable to connect to #{@__host} with user #{@__username}"
|
54
|
+
@__exit_code = 1
|
55
|
+
end
|
56
|
+
|
57
|
+
return false
|
58
|
+
end
|
59
|
+
|
60
|
+
def exec command
|
61
|
+
connect!
|
62
|
+
|
63
|
+
log_str = "#{@__session.options[:user]}@#{@__session.host} -p #{@__session.options[:port]} #{command}"
|
64
|
+
|
65
|
+
@channel = @__session.open_channel do |channel|
|
66
|
+
channel.exec(command) do |ch, success|
|
67
|
+
abort "could not execute #{command} on #{@__session.host}" unless success
|
68
|
+
|
69
|
+
@__output = ''
|
70
|
+
|
71
|
+
channel.on_data do |ch, data|
|
72
|
+
@__output += data
|
73
|
+
end
|
74
|
+
|
75
|
+
channel.on_extended_data do |ch,type,data|
|
76
|
+
@__output += data
|
77
|
+
end
|
78
|
+
|
79
|
+
channel.on_request('exit-status') do |ch, data|
|
80
|
+
@__exit_code = data.read_long
|
81
|
+
end
|
82
|
+
|
83
|
+
# channel.on_request('exit-signal') do |ch, data|
|
84
|
+
# exit_code = data.read_long
|
85
|
+
# end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
@channel.wait
|
91
|
+
@__session.loop
|
92
|
+
|
93
|
+
log_str += "\n" + @__output
|
94
|
+
@__logger.info log_str
|
95
|
+
end
|
96
|
+
|
97
|
+
def output
|
98
|
+
@__output
|
99
|
+
end
|
100
|
+
|
101
|
+
def exit_code
|
102
|
+
@__exit_code
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
class << self
|
108
|
+
def ssh name, config = {}, &block
|
109
|
+
raise "SSH connection '#{name}' not configured" unless @@cfg.key?(name) or config.count > 0
|
110
|
+
|
111
|
+
cfg = @@cfg[name] || {}
|
112
|
+
|
113
|
+
host = cfg['host'] || name
|
114
|
+
username = config[:username] || cfg['username']
|
115
|
+
password = config[:password] || cfg['password']
|
116
|
+
|
117
|
+
opts = {}
|
118
|
+
opts[:password] = password
|
119
|
+
opts[:port] = config[:port] || cfg['port'] || 22
|
120
|
+
opts[:keys] = [cfg['key']] if cfg.key? 'key'
|
121
|
+
opts[:passphrase] = cfg['passphrase'] if cfg.key? 'passphrase'
|
122
|
+
|
123
|
+
opts[:auth_methods] = []
|
124
|
+
opts[:auth_methods].push 'publickey' if opts[:keys]
|
125
|
+
opts[:auth_methods].push 'password' if opts[:password]
|
126
|
+
|
127
|
+
ssh_con = SSHConnection.new(host, username, opts, @@logger)
|
128
|
+
|
129
|
+
begin
|
130
|
+
ssh_con.instance_eval &block
|
131
|
+
ensure
|
132
|
+
ssh_con.close
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
Spectre.register do |config|
|
138
|
+
@@logger = ::Logger.new config['log_file'], progname: 'spectre/ssh'
|
139
|
+
|
140
|
+
if config.key? 'ssh'
|
141
|
+
config['ssh'].each do |name, cfg|
|
142
|
+
@@cfg[name] = cfg
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
Spectre.delegate :ssh, to: self
|
148
|
+
end
|
149
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spectre-ssh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christian Neubauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: openssl
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: spectre-core
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.8.4
|
41
|
+
description: Adds SSH access functionality to the spectre framework
|
42
|
+
email:
|
43
|
+
- me@christianneubauer.de
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/spectre/ssh.rb
|
49
|
+
homepage: https://bitbucket.org/cneubaur/spectre-ssh
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata:
|
53
|
+
allowed_push_host: https://rubygems.org/
|
54
|
+
homepage_uri: https://bitbucket.org/cneubaur/spectre-ssh
|
55
|
+
source_code_uri: https://bitbucket.org/cneubaur/spectre-ssh
|
56
|
+
changelog_uri: https://bitbucket.org/cneubaur/spectre-ssh/src/master/CHANGELOG.md
|
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: 2.5.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.0.8
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: SSH module for spectre
|
76
|
+
test_files: []
|