Wake on Lan

Wake on lan for crystal, usage is dead simple
require “wol”
Wol.wake(“xx:xx:xx:xx:xx:xx”){|sent| if(sent){puts “sent”}

1 Like

Cool! Nice to see it’s that easy (;

I’d recommend using parameters instead of a hash for the options though:
So instead of:

  def wake(mac_address : String, opts : Hash(String,String | Int32) = {"address" => "255.255.255.255","num_packets"=>3,"interval"=>100,"port"=>9,"secure"=>""})
    #... 
  end

Use something like: (the & at the end indicates that it takes a block)

  def wake(mac_address : String, address : String = "255.255.255.255", num_packets = 3, interval = 100, port = 9, secure : String? = nil, &)
    #... 
  end

Also, why the block? Another idea could be to return true/false, instead of forcing the use of a block to get the result

2 Likes

Good suggestion, iv ebeen in JS lang for last 3 months, wol is a refresher and reminder for me. the block is going to be changed so it will return true/false and provide for an optional proc for ip lookup

its been udpated,
(mac_address : String, address : String = “255.255.255.255”, num_packets = 3, interval = 100, port = 9, secure : String? = nil, resolver : Proc(String,_)? = nil)

if you pass a proc it will recieve the ip address from the arp table.
so usage is just
Wol.wake(“xx:xx:xx:xx:xx:xx”)
Wol.wake(“xx:xx:xx:xx:xx:xx”,secure: “xxcceeddffeegg”, port: 7) // alt port and secure wake on lan call.
Wol.wake(“xx:xx:xx:xx:xx:xx”,resolver: (i : String) -> {Puts “IP is #{i}”}) // get the ip from arp, upnp devices with dhcp eacho thier wake on lan mac, so the resovler can be used to update configs with teh new ip as it changes.

1 Like

Thanks for the update!

I have another suggestion, I don’t think the ARP resolution thing should be in wake, in general, functions should do only one thing (and do it well).
First, your implementation uses the tool arp which should be installed by the user of your library, and second, this tool is deprecated (iproute2 is the replacement if you want to take a look).

I think that if you really want to have a way to get IP from mac address, it should be another separate function (not called by wake) that the user can use if they want.

I wrote a basic WOL utility too: https://github.com/acaengine/driver/blob/master/src/engine-driver/utilities/wake_on_lan.cr

Might be something of interest in there

2 Likes

stakach, nice if that was a shard i would have just used it instead

2 Likes