class Miab::Session
Public Class Methods
new( host, ssh=nil, debug: false, dns: '1.1.1.1')
click to toggle source
# File lib/miab.rb, line 42 def initialize( host, ssh=nil, debug: false, dns: '1.1.1.1') @ssh, @host, @debug, @dns = ssh, host, debug, dns puts 'Session dns: ' + dns.inspect if @debug @results = {} end
Public Instance Methods
exec(s)
click to toggle source
# File lib/miab.rb, line 50 def exec(s) eval s @results end
Protected Instance Methods
backup(from: nil, to: nil)
click to toggle source
backup e.g. from: /mnt/usbdisk/gem_src/.
to: pi@192.168.4.158:backup2020/gem_src/.
# File lib/miab.rb, line 61 def backup(from: nil, to: nil) if @debug then puts 'ready to perform backup' puts "from: %s to: %s" % [from, to] end instructions = "rsync -akL -e ssh %s %s" % [from, to] puts 'instructions: ' + instructions if @debug # note: compression is not enabled since this is aimed at # single board computers which have limited CPU capability r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug # since it's running in the background, an empty string will be returned end
date()
click to toggle source
return the local date and time
# File lib/miab.rb, line 84 def date() instructions = 'date' r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug @results[:date] = r.chomp end
directory_exists?(file)
click to toggle source
# File lib/miab.rb, line 93 def directory_exists?(file) instructions = "test -d #{file}; echo $?" r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug @results[:directory_exists?] = r.chomp == '0' end
Also aliased as: dir_exists?
disk_space()
click to toggle source
query the available disk space etc.
# File lib/miab.rb, line 107 def disk_space() instructions = 'df -h' r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` @results[:disk_usage] = {} a = r.lines.grep(/\/dev\/root/) puts ('a: ' + a.inspect).debug if @debug if a.any? then size, used, avail = a[0].split(/ +/).values_at(1,2,3) @results[:disk_usage][:root] = {size: size, used: used, avail: avail} end a2 = r.lines.grep(/\/dev\/sda1/) puts ('a2: ' + a2.inspect).debug if @debug if a2.any? then size, used, avail = a2[0].split(/ +/).values_at(1,2,3) @results[:disk_usage][:sda1] = {size: size, used: used, avail: avail} end end
Also aliased as: df
echo(s)
click to toggle source
return the string supplied
# File lib/miab.rb, line 142 def echo(s) instructions = 'echo ' + s r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug @results[:echo] = r.chomp end
exec_success?(instruction, expected)
click to toggle source
# File lib/miab.rb, line 151 def exec_success?(instruction, expected) r = @ssh ? @ssh.exec!(instruction) : `#{instruction}` puts 'r: ' + r.inspect if @debug @results[:exec_success?] = (r =~ /#{expected}/ ? true : r) end
file_exists?(file)
click to toggle source
# File lib/miab.rb, line 160 def file_exists?(file) instructions = "test -f #{file}; echo $?" r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug @results[:file_exists?] = r == 0 end
file_write(file, content)
click to toggle source
e.g. file_write
'desc.txt', 'Controls the door entry system.'
# File lib/miab.rb, line 172 def file_write(file, content) instructions = "echo #{content.inspect} >> #{file}" r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug @results[:file_write] = r end
install(package)
click to toggle source
return the string supplied
# File lib/miab.rb, line 184 def install(package) return @results[:install] = 'no route to internet' unless internet? return @results[:install] = 'already installed' if installed? package instructions = "apt-get update && apt-get install #{package} -y" r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug @results[:install] = r.chomp end
installable?(package)
click to toggle source
# File lib/miab.rb, line 196 def installable?(package) instructions = "apt-cache search --names-only ^#{package}$" results = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'results: ' + results.inspect if @debug @results[:installable?] = !results.empty? end
installed?(package)
click to toggle source
# File lib/miab.rb, line 206 def installed?(package) instructions = 'dpkg --get-selections | grep -i ' + package results = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'results: ' + results.inspect if @debug return @results[:installed?] = nil if results.empty? r = results.lines.grep /^#{package}/ @results[:installed?] = r.any? end
internet?()
click to toggle source
# File lib/miab.rb, line 218 def internet?() instructions = "ping #{@dns} -W 1 -c 1" r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts 'r: ' + r.inspect if @debug @results[:internet?] = r.lines[1][/icmp_seq/] ? true : false end
memory()
click to toggle source
find out available memory etc
# File lib/miab.rb, line 230 def memory() instructions = 'free -h' puts ('instructions: ' + instructions.inspect).debug if @debug r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` puts ('memory: ' + r.inspect).debug if @debug a = r.lines total, used, avail = a[1].split.values_at(1,2,-1) @results[:memory] = {total: total, used: used, available: avail} end
ping()
click to toggle source
query the ping time
# File lib/miab.rb, line 245 def ping() ip = Resolv.getaddress(@host) puts ('ip: ' + ip.inspect).debug if @debug valid = pingecho(ip) puts ('valid: ' + valid.inspect).debug if @debug @results[:ping] = if valid then a = [valid] 4.times {sleep 0.01; a << pingecho(ip)} (a.min * 1000).round(3) else nil end end
pwd()
click to toggle source
query the path of the current working directory
# File lib/miab.rb, line 264 def pwd() instructions = 'pwd' r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` @results[:pwd] = r.chomp end
temperature()
click to toggle source
query the CPU temperature
# File lib/miab.rb, line 272 def temperature() instructions = 'cat /sys/class/thermal/thermal_zone0/temp' r = @ssh ? @ssh.exec!(instructions) : `#{instructions}` @results[:temperature] = r.chomp end
Private Instance Methods
pingecho(host, timeout=5, service="echo")
click to toggle source
# File lib/miab.rb, line 281 def pingecho(host, timeout=5, service="echo") elapsed = nil time = Time.new begin Timeout.timeout(timeout) do s = TCPSocket.new(host, service) s.close end rescue Errno::ECONNREFUSED return Time.now - time rescue Timeout::Error, StandardError return false end # it should not reach this far return true end