GR-PEACH nicモジュール (nic)

nic モジュールは、ネットワーク環境を制御するためのモジュールです。

使用例

var nic = require('nic');
var nics = nic.enumerate();
console.log( Object.keys( nics ).length + "個のNICが搭載されています" );
var index = 0;
Object.keys( nics ).forEach( function(key) {
    console.log( ++ index + "個目は:" + key );
});

var eth0 = nics["ETHERNET"];
if( eth0 ) {
    eth0.ifup();        // use DHCP
    eth0.ntpdate({ server:"ntp.nict.jp" });
    console.log( JSON.stringify( eth0.ifconfig() ) );
    console.log( new Date().toString() );
}

nic.enumerate()

ボードに搭載された NIC 一覧を取得します。(現在、Ethernet Interface のみに対応しています)

使用例

var nic = require('nic');

// ★★ 一覧取得
var nics = nic.enumerate();

// 一覧表示
console.log( Object.keys( nics ).length + "個のNICが搭載されています" );
var index = 0;
Object.keys( nics ).forEach( function(key) {
    console.log( ++ index + "個目は:" + key );
});

Nicクラス

NIC の制御や状態の取得を行うクラスです。

nic.ifup([param])

ネットワークへ接続します。 paramを指定しない場合、およびip, netmask, gatewayのどれか一つでも省略されている場合はDHCPを使用します。 ip, netmask, gatewayが全て指定されている場合は静的IPアドレスを使用します。 dnsはDHCP・静的IPアドレスのどちらを使用する場合でも省略可能です。

使用例

var nic = require('nic');
var nics = nic.enumerate();
var eth0 = nics["ETHERNET"];
if( !eth0 ) {
    return;
}

// ★★ ネットワークへ接続 (DHCP)
eth0.ifup();
eth0.ifdown();

// ★★ ネットワークへ接続 (静的IPアドレス指定)
eth0.ifup({
    ip: "192.168.123.100",
    netmask: "255.255.255.0",
    gateway: "192.168.123.1",
    dns: "8.8.8.8"
});
eth0.ifdown();

nic.ifdown()

ネットワークから切断します。

使用例

var nic = require('nic');
var nics = nic.enumerate();
var eth0 = nics["ETHERNET"];
if( !eth0 ) {
    return;
}
eth0.ifup();

// ★★ ネットワークから切断
eth0.ifdown();

nic.ifconfig()

NICの状態を取得します。

使用例

var nic = require('nic');
var nics = nic.enumerate();
var eth0 = nics["ETHERNET"];
if( !eth0 ) {
    return;
}
eth0.ifup();

// ★★ NIC の状態を取得 (接続時)
var conf = eth0.ifconfig();
console.log( JSON.stringify( conf ) );

eth0.ifdown();

// ★★ NIC の状態を取得 (切断時)
conf = eth0.ifconfig();
console.log( JSON.stringify( conf ) );

nic.ntpdate([param])

インターネット時刻と同期します。

使用例

var nic = require('nic');
var nics = nic.enumerate();
var eth0 = nics["ETHERNET"];
if( !eth0 ) {
    return;
}
eth0.ifup();

// ★★ インターネット時刻と同期(デフォルトサーバ "0.pool.ntp.org" 使用)
if( eth0.ntpdate() ) {
    console.log( '時刻同期成功', new Date().toString() );
}
else {
    console.log( '時刻同期失敗' );
}

// ★★ インターネット時刻と同期(サーバ指定)
if( eth0.ntpdate({ server:"ntp.nict.jp" }) ) {
    console.log( '時刻同期成功', new Date().toString() );
}
else {
    console.log( '時刻同期失敗' );
}

eth0.ifdown();