-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
315 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
|
||
|
||
module.exports = function (cp) { | ||
return function (f) { | ||
|
||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
|
||
var MAC = 'HWaddr'; | ||
var INET = 'inet'; | ||
var BCAST = 'Bcast'; | ||
|
||
module.exports = function (cp) { | ||
return function (f) { | ||
// @todo add command timeout | ||
cp.exec('ifconfig', function (err, ifConfigOut, stderr) { | ||
if (err) { | ||
return f(err); | ||
} | ||
|
||
if (stderr) { | ||
return f(stderr); | ||
} | ||
|
||
cp.exec('route', function (err, routeOut, stderr) { | ||
if (err) { | ||
return f(err); | ||
} | ||
|
||
if (stderr) { | ||
return f(stderr); | ||
} | ||
|
||
f(null, parse(ifConfigOut, routeOut)); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
|
||
function parse(ifConfigOut, routeOut) { | ||
return ifConfigOut.split('\n\n').map(function (inface) { | ||
var lines = inface.split('\n'); | ||
|
||
/** | ||
* Format 1 | ||
* link xx:xx HWaddr xx-xx-xx | ||
* link xx:xx HWaddr xx:xx:xx | ||
* | ||
* Format 1 | ||
* inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx | ||
*/ | ||
|
||
return { | ||
name: getInterfaceName(_.first(lines)), | ||
ip: getInterfaceIpAddr(lines[1]), | ||
netmask: getInterfaceNetmaskAddr(lines[1]), | ||
broadcast: getBroadcastAddr(lines[1]), | ||
mac: getInterfaceMacAddr(_.first(lines)), | ||
gateway: getGateway(routeOut) | ||
}; | ||
}); | ||
} | ||
|
||
function getInterfaceName(firstLine) { | ||
return _.first(firstLine.split(' ')); | ||
} | ||
|
||
/** | ||
* extract mac adress | ||
* | ||
* ifconfig output: | ||
* - link xx:xx HWaddr xx-xx-xx | ||
* - link xx:xx HWaddr xx:xx:xx | ||
* | ||
* @param {string} firstLine | ||
* @return {string} Mac address, format: "xx:xx:xx:xx:xx:xx" | ||
*/ | ||
function getInterfaceMacAddr(firstLine) { | ||
if (!_.include(firstLine, MAC)) { | ||
return null; | ||
} | ||
|
||
var macAddr = _.last(firstLine.split(MAC)).trim().replace(/-/g, ':'); | ||
|
||
if (macAddr.split(':').length !== 6) { | ||
return null; | ||
} | ||
|
||
return macAddr; | ||
} | ||
|
||
/** | ||
* extract ip addr | ||
* | ||
* ifconfig output: | ||
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx | ||
* | ||
* @param {string} line | ||
* @return {string,null} xxx.xxx.xxx.xxx | ||
*/ | ||
function getInterfaceIpAddr(line) { | ||
if (!_.include(line, INET)) { | ||
return null; | ||
} | ||
return _.first(line.split(':')[1].split(' ')); | ||
} | ||
|
||
/** | ||
* extract netmask addr | ||
* | ||
* ifconfig output: | ||
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx | ||
* | ||
* @param {string} line | ||
* @return {string,null} xxx.xxx.xxx.xxx | ||
*/ | ||
function getInterfaceNetmaskAddr(line) { | ||
if (!_.include(line, INET)) { | ||
return null; | ||
} | ||
return _.last(line.split(':')); | ||
} | ||
|
||
/** | ||
* extract broadcast addr | ||
* @param {string} line | ||
* @return {string,null} xxx.xxx.xxx.xxx | ||
*/ | ||
function getBroadcastAddr(line) { | ||
if (!_.include(line, BCAST)) { | ||
return null; | ||
} | ||
|
||
// inet adr:1.1.1.77 Bcast:1.1.1.255 Masque:1.1.1.0 | ||
return _.chain(line) | ||
.split(BCAST) | ||
.slice(1) | ||
.first() | ||
.value() | ||
.substring(1) | ||
.split(' ')[0]; | ||
} | ||
|
||
|
||
/** | ||
* extract gateway ip | ||
* @param {string} stdout | ||
* @return {string,null} default gateway ip or null | ||
*/ | ||
function getGateway(stdout) { | ||
// @todo this is ugly. | ||
return _.chain(stdout) | ||
.split('\n') | ||
.filter(function (line) { | ||
return _.include(line, 'default'); | ||
}) | ||
.first() | ||
.split(' ') | ||
.rest() // without "default" | ||
.join('') | ||
.split('.') | ||
.first() | ||
.trim() | ||
.replace(/-/g, '.') | ||
.value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,8 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
|
||
module.exports = function (cp) { | ||
return { | ||
interfaces: function (f) { | ||
cp.exec('ifconfig', function (err, stdout, stderr) { | ||
if (err) { | ||
return f(err); | ||
} | ||
|
||
if (stderr) { | ||
return f(stderr); | ||
} | ||
|
||
f(null, parse(stdout)); | ||
}); | ||
} | ||
interfaces: require('./ifconfig.interfaces')(cp), | ||
configure: require('./ifconfig.configure')(cp) | ||
}; | ||
}; | ||
|
||
|
||
function parse(stdout) { | ||
return stdout.split('\n\n').map(function (inface) { | ||
var lines = inface.split('\n'); | ||
|
||
/** | ||
* Format 1 | ||
* link xx:xx HWaddr xx-xx-xx | ||
* link xx:xx HWaddr xx:xx:xx | ||
* | ||
* Format 1 | ||
* inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx | ||
*/ | ||
|
||
return { | ||
name: getInterfaceName(_.first(lines)), | ||
ip_address: getInterfaceIpAddr(lines[1]), | ||
netmask: getInterfaceNetmaskAddr(lines[1]), | ||
mac_address: getInterfaceMacAddr(_.first(lines)) | ||
}; | ||
}); | ||
} | ||
|
||
function getInterfaceName(firstLine) { | ||
return _.first(firstLine.split(' ')); | ||
} | ||
|
||
/** | ||
* extract mac adress | ||
* | ||
* ifconfig output: | ||
* - link xx:xx HWaddr xx-xx-xx | ||
* - link xx:xx HWaddr xx:xx:xx | ||
* | ||
* @param {string} firstLine | ||
* @return {string} Mac address, format: "xx:xx:xx:xx:xx:xx" | ||
*/ | ||
var MAC = 'HWaddr'; | ||
|
||
function getInterfaceMacAddr(firstLine) { | ||
if (!_.include(firstLine, MAC)) { | ||
return null; | ||
} | ||
|
||
var macAddr = _.last(firstLine.split(MAC)).trim().replace(/-/g, ':'); | ||
|
||
if (macAddr.split(':').length !== 6) { | ||
return null; | ||
} | ||
|
||
return macAddr; | ||
} | ||
|
||
/** | ||
* extract ip addr | ||
* | ||
* ifconfig output: | ||
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx | ||
* | ||
* @param {string} line | ||
* @return {string} xxx.xxx.xxx.xxx | ||
*/ | ||
|
||
var INET = 'inet'; | ||
|
||
function getInterfaceIpAddr(line) { | ||
if (!_.include(line, INET)) { | ||
return null; | ||
} | ||
return _.first(line.split(':')[1].split(' ')); | ||
} | ||
|
||
/** | ||
* extract netmask addr | ||
* | ||
* ifconfig output: | ||
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx | ||
* | ||
* @param {string} line | ||
* @return {string} xxx.xxx.xxx.xxx | ||
*/ | ||
function getInterfaceNetmaskAddr(line) { | ||
if (!_.include(line, INET)) { | ||
return null; | ||
} | ||
return _.last(line.split(':')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Table de routage IP du noyau | ||
Destination Passerelle Genmask Indic Metric Ref Use Iface | ||
10.21.21.112 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.113 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.114 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.100 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.102 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.103 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.104 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.105 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.107 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.109 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.110 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.21.21.111 * 255.255.255.255 UH 0 0 0 venet0 | ||
10.10.10.0 * 255.255.255.0 U 0 0 0 vmbr0 | ||
10.21.21.0 * 255.255.255.0 U 0 0 0 vmbr2 | ||
default 10-10-10-1.re 0.0.0.0 UG 0 0 0 vmbr0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Table de routage IP du noyau | ||
Destination Passerelle Genmask Indic Metric Ref Use Iface | ||
default 10-10-10-1.po 0.0.0.0 UG 0 0 0 eth0 | ||
10.10.10.0 * 255.255.255.0 U 0 0 0 eth0 |
Oops, something went wrong.