Skip to content

Instantly share code, notes, and snippets.

@abronte
Created January 5, 2012 00:26
Show Gist options
  • Select an option

  • Save abronte/1563033 to your computer and use it in GitHub Desktop.

Select an option

Save abronte/1563033 to your computer and use it in GitHub Desktop.
Machine model
require 'ping'
require 'socket'
require 'net/ssh'
require 'wol.rb'
class Machine < ActiveRecord::Base
def alive?
if Ping.pingecho(self.ip, 2)
self.mstatus = "up"
self.last_alive = Time.now
self.save
true
else
self.mstatus = "down"
self.save
false
end
end
def wake
m = WakeOnLan.new({:ip => self.ip, :mac => self.mac})
m.wake
end
def sleep
Thread.new {
if self.mtype == 'linux' && self.mstatus == "up"
Net::SSH.start(self.ip, self.user, :password => self.password) do |ssh|
ssh.exec('halt -p')
end
elsif self.mtype == 'windows' && self.mstatus == "up"
Net::SSH.start(self.ip, self.user, :password => self.password) do |ssh|
ssh.exec('shutdown /h')
end
end
Kernel.sleep(15)
Thread.exit
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment