Skip to content

Instantly share code, notes, and snippets.

@zunda
Last active February 14, 2020 07:41
Show Gist options
  • Save zunda/187139 to your computer and use it in GitHub Desktop.
Save zunda/187139 to your computer and use it in GitHub Desktop.
A ruby script which sends out a magic packet to wake up your PC
#!/usr/bin/ruby
# wol.rb: sends out a magic packet to wake up your PC
#
# Copyright (c) 2004 zunda <zunda at freeshell.org>
#
# This program is free software. You can re-distribute and/or
# modify this program under the same terms of ruby itself ---
# Ruby Distribution License or GNU General Public License.
#
# target machine
mac = 'XX:XX:XX:XX:XX:XX' # hex numbers
# target network
host = '192.168.X.X'
local = true
# host = 'example.com'
# local = false
require 'socket'
port = 9 # Discard Protocol
message = "\xFF".force_encoding(Encoding::ASCII_8BIT)*6 + [ mac.gsub( /:/, '' ) ].pack( 'H12' )*16
txbytes = UDPSocket.open do |so|
if local then
so.setsockopt( Socket::SOL_SOCKET, Socket::SO_BROADCAST, true )
end
so.send( message, 0, host, port )
end
puts "#{txbytes} bytes sent to #{host}:#{port}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment