miab 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/miab.rb +126 -6
  5. metadata +2 -2
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3496f53c24051559de73b16b3ec351a6f6c7054b04e72e6cdde7851f43e41428
4
- data.tar.gz: b3617027ca5ef79dfdfe06f790fb7fb048a97140096a246ddf38266babecd2bc
3
+ metadata.gz: ced57340f07efffc93c646a147121deb9a1fbb8d037aaa37ba2eb3203fc69573
4
+ data.tar.gz: 3ff78cd963d512efa0528cbee9456b8efed970a943698db326bf3ff95717a60d
5
5
  SHA512:
6
- metadata.gz: 8cc9a8aef7ec3f22f45cae23015b8709a05390a5100b6498378190b15e34bdeca2dfa6d4a9fa45ac70343b20e91ab5c164678f376cfc20fab54e4a8558f2c071
7
- data.tar.gz: 13f991b278fc3d8b077464120abf3cc7184439e4870f122d49d8df42fd8332eca7fbf0b73ed3534d403da17b40f391653476fc5f007267f7e52f744d9ce6c532
6
+ metadata.gz: 0ee3e715313c423032f22db63c8f9e8a21bd33877960abdcea64ba22b2ab44d5cd5de5519c485895af819d0590f79e0082d584d4cadf3ee88532ccbd605ab129
7
+ data.tar.gz: a8aae9b45ea47714cc4ca79562d5c904ee1592b8daa73989191ae22500a8185af3c7bd611e618610d04fa037a3622d738e6b27e8bed3c2138ecf8aee7543b838
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/miab.rb CHANGED
@@ -4,19 +4,45 @@
4
4
 
5
5
  # Desc: Message in a bottle (MIAB) is designed to execute remote commands
6
6
  # through SSH for system maintenance purposes.
7
+ #
8
+ # Note: Intended for a Debian based distro
7
9
 
8
10
  require 'net/ssh'
9
11
  require 'c32'
10
12
  require 'resolv'
11
13
 
12
14
 
15
+ # available commands:
16
+ #
17
+ # * date - returns the system date and time
18
+ # * directory_exists? - returns true if the directory exists
19
+ # * disk_space - returns the available disk space
20
+ # * echo - returns whatever string is passed in
21
+ # * file_exists? - returns true if the file exists
22
+ # * file_write - writes contents to a file
23
+ # * installable? - returns true if the package name exists in apt-cache search
24
+ # * installed? - returns true if a package is already installed
25
+ # * internet? - returns true if a ping request to an external IP address succeeds
26
+ # * memory - returns the amount of free RAM available etc
27
+ # * ping - returns the latency of a ping request to the node
28
+ # * pwd - returns the working directory
29
+ # * temperature - returns the temperature of the CPU
30
+
31
+ # usage: puts Miab.new("temperature", domain: 'home', target: 'angelo',
32
+ # user: 'pi', password: 'secret').cast
33
+ # nearest SSH equivalent `ssh pi@angelo.home exec \
34
+ # "cat /sys/class/thermal/thermal_zone0/temp"`
35
+
13
36
  class Miab
14
37
  using ColouredText
15
38
 
16
39
  class Session
17
40
 
18
- def initialize( host, ssh=nil, debug: false)
19
- @ssh, @host, @debug = ssh, host, debug
41
+ def initialize( host, ssh=nil, debug: false, dns: '1.1.1.1')
42
+
43
+ @ssh, @host, @debug, @dns = ssh, host, debug, dns
44
+
45
+ puts 'Session dns: ' + dns.inspect if @debug
20
46
  @results = {}
21
47
  end
22
48
 
@@ -37,6 +63,18 @@ class Miab
37
63
  @results[:date] = r.chomp
38
64
 
39
65
  end
66
+
67
+ def directory_exists?(file)
68
+
69
+ instructions = "test -d #{file}; echo $?"
70
+ r = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
71
+ puts 'r: ' + r.inspect if @debug
72
+
73
+ @results[:directory_exists?] = r.chomp == '0'
74
+
75
+ end
76
+
77
+ alias dir_exists? directory_exists?
40
78
 
41
79
  # query the available disk space etc.
42
80
  #
@@ -72,6 +110,85 @@ class Miab
72
110
  end
73
111
 
74
112
  alias df disk_space
113
+
114
+ # return the string supplied
115
+ #
116
+ def echo(s)
117
+
118
+ instructions = 'echo ' + s
119
+ r = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
120
+ puts 'r: ' + r.inspect if @debug
121
+ @results[:echo] = r.chomp
122
+
123
+ end
124
+
125
+ def file_exists?(file)
126
+
127
+ instructions = "test -f #{file}; echo $?"
128
+ r = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
129
+ puts 'r: ' + r.inspect if @debug
130
+
131
+ @results[:file_exists?] = r == 0
132
+
133
+ end
134
+
135
+ # e.g. file_write 'desc.txt', 'Controls the door entry system.'
136
+ #
137
+ def file_write(file, content)
138
+
139
+ instructions = "echo #{content.inspect} >> #{file}"
140
+ r = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
141
+ puts 'r: ' + r.inspect if @debug
142
+
143
+ @results[:file_write] = r
144
+
145
+ end
146
+
147
+ # return the string supplied
148
+ #
149
+ def install(package)
150
+
151
+ return @results[:install] = 'no route to internet' unless internet?
152
+ return @results[:install] = 'already installed' if installed? package
153
+
154
+ instructions = "apt-get update && apt-get install #{package} -y"
155
+ r = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
156
+ puts 'r: ' + r.inspect if @debug
157
+ @results[:install] = r.chomp
158
+
159
+ end
160
+
161
+ def installable?(package)
162
+
163
+ instructions = "apt-cache search --names-only ^#{package}$"
164
+ results = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
165
+ puts 'results: ' + results.inspect if @debug
166
+
167
+ @results[:installable?] = !results.empty?
168
+
169
+ end
170
+
171
+ def installed?(package)
172
+
173
+ instructions = 'dpkg --get-selections | grep -i ' + package
174
+ results = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
175
+ puts 'results: ' + results.inspect if @debug
176
+
177
+ return @results[:installed?] = nil if results.empty?
178
+ r = results.lines.grep /^#{package}/
179
+
180
+ @results[:installed?] = r.any?
181
+ end
182
+
183
+ def internet?()
184
+
185
+ instructions = "ping #{@dns} -W 1 -c 1"
186
+ r = @ssh ? @ssh.exec!(instructions) : `#{instructions}`
187
+ puts 'r: ' + r.inspect if @debug
188
+
189
+ @results[:internet?] = r.lines[1][/icmp_seq/] ? true : false
190
+
191
+ end
75
192
 
76
193
  # find out available memory etc
77
194
  #
@@ -152,11 +269,13 @@ class Miab
152
269
  end
153
270
 
154
271
  def initialize(scroll, domain: nil, target: nil, pwlist: {}, password: nil,
155
- user: nil, debug: false)
272
+ user: nil, debug: false, dns: '208.67.222.222')
156
273
 
157
274
  @results = {}
158
275
 
159
- @scroll, @debug = scroll, debug
276
+ @scroll, @debug, @dns = scroll, debug, dns
277
+
278
+ puts '@dns: ' + @dns.inspect if @debug
160
279
 
161
280
  @nodes = if target then
162
281
 
@@ -181,6 +300,7 @@ class Miab
181
300
  if @nodes.any? then
182
301
 
183
302
  threads = []
303
+ dns = @dns
184
304
 
185
305
  @nodes.each do |raw_host, password|
186
306
 
@@ -191,7 +311,7 @@ class Miab
191
311
  begin
192
312
  puts ('host: ' + host.inspect).debug if @debug
193
313
  ssh = Net::SSH.start( host, user, password: password)
194
- @results[host] = Session.new(host, ssh, debug: @debug).exec @scroll
314
+ @results[host] = Session.new(host, ssh, dns: dns, debug: @debug).exec @scroll
195
315
  ssh.close
196
316
  puts (host + ' result: ' + @results[host].inspect).debug if @debug
197
317
  rescue
@@ -207,7 +327,7 @@ class Miab
207
327
 
208
328
  if @scroll then
209
329
  host = `hostname`.chomp
210
- @results[host] = Session.new(host, debug: @debug).exec(@scroll)
330
+ @results[host] = Session.new(host, dns: @dns, debug: @debug).exec(@scroll)
211
331
  end
212
332
 
213
333
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  9irKsAp/hZt3dTbQOtnSlc9XREZZdegwOgu1FEqBqviNIn9R28OR237HExiWXwmw
36
36
  HDTFIjk8XBqTunABuFUgr4qx
37
37
  -----END CERTIFICATE-----
38
- date: 2019-09-16 00:00:00.000000000 Z
38
+ date: 2019-11-26 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: net-ssh
metadata.gz.sig CHANGED
Binary file