Skip to main content

Java-jruby program to turn on computers remotely

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

Popular posts from this blog

Mail labels and letter templates for jasperreports

The following are free (MIT license) mailing labels and letter templates for jasperreports that you can download and use in jasperserver and/or ireport: Update 3/15/2011 : I moved the Mail templates zip file here . Please consider making a small donation if the templates are of help to you, Thank you! If you need more information on how to use those templates please leave a comment in the blog.

How to create online multiplayer HTML5 games in Contruct2

  Construct2 can use websockets to send and receive messages between games. By using socket-io , we can use a Node.js script as the server and my modification to the socket-io plugin for Construct2 to allow the games to synchronize data between them in real-time. There are two parts to this design: the Node.js server and the Construct2 clients (the games playing). The main part of building an online multiplayer HTML5 game is to plan: how the clients will communicate how often and what to communicate how much of the logic will go into the server and how much to the client. In my sample game, I chose to have each client own a player and have the server just relay messages: Use string messages in the form TypeOfMessage, Parameter1, Paremeter2, Parater3, etc to communicate. Have the clients send their player position about 16 times a second. Whenever their player shoots, the client needs to send a message immediately. Almost all of the game logic will...

Send Email from C# using Outlook's COM late binding

The following sample code shows how to send emails from Outlook and Exchange using C#. This code works with any version of Outlook because it uses Late Binding to automate Outlook. Parts of the code where taken from other websites. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Threading; namespace LateBindingTest { class OutlookEmailerLateBinding { private object oApp; private object oNameSpace; private object oOutboxFolder; public OutlookEmailerLateBinding() { Type outlook_app_type; object[] parameter = new object[1]; //Get the excel object outlook_app_type = Type.GetTypeFromProgID("Outlook.Application"); //Create instance of excel oApp = Activator.CreateInstance(outlook_app_type); //Set the parameter which u want to set parameter[0] = "MAPI...