8
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsでWake On LANコマンドを作る

Last updated at Posted at 2012-08-23

モジュールnode_wake_on_lan を試してみた。
https://github.com/agnat/node_wake_on_lan

インストール

$ npm install wake_on_lan

var wol = require('wake_on_lan');
wol.wake('20:DE:20:DE:20:DE');

準備

  1. BIOS等で"Wake On LAN" を設定する
  2. NICの "Wake On LAN" サポートを確認
  3. 起こしたいマシンのMacアドレスを調べておく

やってみる

下記のようなshebang付きnodeコードを書いてwol.jsとかの名前で保存し、実行権限を与える

wol.js

# !/usr/bin/env node

var wol = require('wake_on_lan');

//起こしたいマシンのMacアドレスを調べてリストアップしておく
var lists = {
  '1': '80:ee:73:xx:xx:xx', //192.168.1.1
  '2': '80:ee:73:xx:xx:xx'  //192.168.1.2
  
};

//引数まわして起動
process.argv.forEach(function (val, index, array) {
  if(index < 2)return;
  wakeUp (val);
});

//マジックパケット送信
function wakeUp (val){
  var mac = lists[val];
  if(!mac){
    console.log(val + ' はリストにありません');
    return; 
  }
  console.log('処理中: ' + val + ': ' + mac);
  
  wol.wake(mac, function(error) {
    if (error) {
      console.log(error);
    } else {
      // done sending packets
      console.log('マジックパケットを送りました: ' + mac );
    }
  });
}

do it !

hoge@myserver:~$ /home/hoge/wol.js 1
処理中: 1: 80:ee:73:xx:xx:xx
マジックパケットを送りました: 80:ee:73:xx:xx:xx

で、192.168.1.1が起動しました。

8
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?