Using wakeonlan.rb from Kevin R. Bullock, I was able to creat a small jruby swing-gui program for turning on remote computers using Wake On LAN. The program displays in an array buttons corresponding to the computers listed in a text file. When you click the button, the program sends a WOL packet to the computer to turn it on. The code is the following (note that you need wakeonlan.rb in the same folder):
# Copyright (c) 2008 Juan Pablo Tarquino
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
include Java
import javax.swing.JFrame
require 'wakeonlan.rb'
class JptWOL
@@wol = WakeOnLAN.new
#Gets a list of MAC addresses from a file with labels and
# MAC addresses (separated by a tab) on each line
# e.g.:
# PC_11[tab]00-14-85-09-E2-03
# PC_22[tab]00-16-17-DE-93-05
def self.get_mac_addresses_from_file(filename)
in_file = File.open(filename)
mac_addresses = in_file.read.gsub("-",":").split("\n")
in_file.close
return mac_addresses
end
def self.wake_pc(mac_address)
begin
@@wol.setup( mac_address )
$stderr.print "Sending wake packet for #{mac_address}"
$stderr.print "..."
@@wol.send_wake()
$stderr.print "done.\n"
rescue WakeOnLAN::AddressException
$stderr.puts $!
#exit(1)
rescue
$stderr.print "\nError: #{$!}\n"
#exit(1)
end
end
def self.wake_from_file(filename = "mac_addresses.txt")
outfile = File.open("log.txt", "w+")
$stderr = outfile
$stderr.puts("Starting WOL script at #{Time.new.to_s}")
mac_addresses = get_mac_addresses_from_file(filename)
mac_addresses.each do |addr|
wake_pc(addr)
end
outfile.close
end
def self.open_dialog
frame = JFrame.new "WOL"
#You may need to edit he dimensions of the grid if you
# have more than 100 (5 x 20) computers to show on the screen
frame.content_pane.layout = java.awt.GridLayout.new(5, 20)
frame.default_close_operation = JFrame::EXIT_ON_CLOSE
mac_addresses_raw = get_mac_addresses_from_file("macs.txt")
pcs_info = []
mac_addresses_raw.each do |info|
puts info
info_pair = info.split("\t")
#pair definition: name_for_pc\tMACaddress
pcs_info << info_pair
b = javax.swing.JButton.new(info_pair[0])
b.add_action_listener { |evt| wake_pc(info_pair[1]) };
frame.add b
end
frame.pack
frame.visible = true
end
end
JptWOL.open_dialog
Comments