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

Powershell script for converting JPG to TIFF

The following Powershell script will convert a batch of JPEG files to TIFF format: #This Code is released under MIT license [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $files_folder = 'C:\path-where-your-jpg-files-are\' $pdfs = get-childitem $files_folder -recurse | where {$_.Extension -match "jpg"} foreach($pdf in $pdfs) { $picture = [System.Drawing.Bitmap]::FromFile( $pdf.FullName ) $tiff = $pdf.FullName.replace('.PDF','').replace('.pdf','').replace('.jpg','').replace('.JPG','') + '.tiff' $picture.Save($tiff) }

Power Automate: SFTP action "Test connection failed"

When I added an SFTP create file action to my Power Automate flow ( https://flow.microsoft.com ) , I got the following error in the action step, within the designer: "Test connection failed" To troubleshoot the Power Automate connection, I had to: go the Power Automate portal then "Data"->"Connections"  the sftp connection was there, I clicked on the ellipsis, and entered the connection info It turns out, that screen provides more details about the connection error. In my case, it was complaining that "SSH host key finger-print xxx format is not supported. It must be in 'MD5' format". I had provided the sha fingerprint that WinScp shows. Instead, I needed to use the MD5 version of the fingerprint. To get that, I had to run in command line (I was in a folder that had openssh in it): ssh -o FingerprintHash=md5 mysftpsite.com To get the fingerprint in MD5 format. I took the string (without the "MD5:" part of the string) and put

Alert if file missing using Powershell

The following Powershell script can be used to send an email alert when a file is missing from a folder or it is the same file from a previous check: $path_mask = "yourfile_*.txt" $previous_file_store = "lastfileread.txt" $script_name = "File Check" ###### Functions ########## Function EMailLog($subject, $message) {    $emailTo = "juanito@yourserver.com"    $emailFrom = "alert@yourserver.com"    $smtpserver="smtp.yourserver.com"       $smtp=new-object Net.Mail.SmtpClient($smtpServer)    $smtp.Send($emailFrom, $emailTo, $subject, $message) } Try {    #get files that match the mask    $curr_file = dir $path_mask |  select name    if ($curr_file.count -gt 0)    {        #file found        #check if the file is different from the previous file read        $previous_file = Get-Content $previous_file_store        $curr_file_name = $curr_file.Item(0).Name        if ($