Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial checkin of wake on lan demo code

  • Loading branch information...
commit cf0ef1ea33aeefd8b500884108a669aa8f2973d4 0 parents
@agnat authored
Showing with 65 additions and 0 deletions.
  1. +4 −0 README.textile
  2. +27 −0 wake_on_lan.js
  3. +34 −0 wol.js
4 README.textile
@@ -0,0 +1,4 @@
+h1. README
+
+A simple wake on lan packet generator for node.js
+
27 wake_on_lan.js
@@ -0,0 +1,27 @@
+var sys = require('sys'),
+ Buffer = require('buffer').Buffer;
+
+exports.createMagicPacket = function(mac) {
+ var num_mac_octets = 6;
+ if (mac.length == 2 * num_mac_octets + 5) {
+ var sep = mac[2];
+ mac = mac.replace(new RegExp(sep, 'g'), '');
+ } else if (mac.length != 2 * num_mac_octets) {
+ throw new Error("malformed MAC address '" + mac + "'");
+ }
+
+ var mac_buffer = new Buffer(num_mac_octets);
+ for (var i = 0; i < num_mac_octets; ++i) {
+ mac_buffer[i] = parseInt(mac.substr(2 * i, 2), 16);
+ }
+
+ var num_macs = 16;
+ var buffer = new Buffer((1 + num_macs) * num_mac_octets);
+ for (var i = 0; i < num_mac_octets; ++i) {
+ buffer[i] = 0xff;
+ }
+ for (var i = 0; i < num_macs; ++i) {
+ mac_buffer.copy(buffer, (i + 1) * num_mac_octets, 0, mac_buffer.length)
+ }
+ return buffer;
+};
34 wol.js
@@ -0,0 +1,34 @@
+var sys = require('sys'),
+ dgram = require('dgram'),
+ wol = require('./wake_on_lan');
+
+if (process.argv.length != 3) {
+ sys.puts('Usage: ' + process.argv[0] + " " + process.argv[1] + " <MAC>");
+ process.exit(1);
+}
+
+var mac = process.argv[2];
+
+var magic_packet = wol.createMagicPacket(mac);
+
+
+var address = "255.255.255.255";
+var socket = dgram.createSocket(true);
+var done_callback = function() {
+ socket.close();
+};
+
+var i = 0;
+var num_packets = 3;
+var packet_interval = 100; //ms
+sys.puts("sending " + num_packets + " WOL packets to " + mac);
+function sendWOL() {
+ ++i;
+ socket.send(4143, address, magic_packet, 0, magic_packet.length,
+ i < num_packets ? null : done_callback);
+ if (i < num_packets) {
+ setTimeout(sendWOL, packet_interval);
+ }
+}
+sendWOL();
+
Please sign in to comment.
Something went wrong with that request. Please try again.