Skip to content

Instantly share code, notes, and snippets.

@mraaroncruz
Forked from scharfie/wol.rb
Last active June 15, 2018 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mraaroncruz/07d128d4d709f08b34060bbd27d169c4 to your computer and use it in GitHub Desktop.
Save mraaroncruz/07d128d4d709f08b34060bbd27d169c4 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>
# Modified by scharfie <scharfie at gmail dot com>
#
# 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.
#
require 'socket'
mac = '6C:F0:49:E3:DE:85' # hex numbers
network = '255.255.255.255'
local = true
port = 4343
# message = "\xFF"*6 + [mac.gsub(/:/, '')].pack('H12')*16
message = "\xFF".force_encoding(Encoding::ASCII_8BIT)*6 + [ mac.gsub( /:/, '' ) ].pack( 'H12' )*16
txbytes = UDPSocket.open do |so|
so.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
so.send(message, 0, network, port)
end
puts "#{txbytes} bytes sent to #{network}:#{port}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment