Created
January 5, 2012 00:26
-
-
Save abronte/1563033 to your computer and use it in GitHub Desktop.
Machine model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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