Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter input data by network adapter name for #2 #22

Merged
merged 2 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/lib/getmac.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,49 @@ isWindows = process.platform.indexOf('win') is 0
macRegex = /(?:[a-z0-9]{2}[:\-]){5}[a-z0-9]{2}/ig
zeroRegex = /(?:[0]{2}[:\-]){5}[0]{2}/

# Filter By Interface
# filterByInterface(iface, str)
filterByInterface = (iface, str) ->
iface = new RegExp("#{iface}[:\\s]")
lines = str.split(/\r?\n/g)
result = ''
padding = null

for line in lines
if result.length is 0
result += line if iface.test(line)
continue
else if padding is null
match = /^(\s+)/.exec(line)
if match
result += "\n#{line}"
padding = new RegExp("^#{match[1]}")
continue
else
match = padding.exec(line)
if match
result += "\n#{line}"
continue
break

result

# Get Mac
# next(err,macAddress)
getMac = (opts, next) ->
# Prepare
[opts, next] = extractOptsAndCallback(opts, next)
{data} = opts
{data, iface} = opts
data ?= null
iface ?= null

# Command
command = if isWindows then "getmac" else "ifconfig -a || ip link"

# Extract Mac
extractMac = (data, next) ->
# Prepare
data = filterByInterface(iface, data) if iface
result = null

# Find a valid mac address
Expand Down
21 changes: 21 additions & 0 deletions src/test/everything-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ joe.describe 'getmac', (describe,it) ->
expect(macAddress).to.eql('00:18:31:8A:41:C6')
return done()

it 'got the mac address of eth0 successfully', (done) ->
getMac {data, iface: 'eth0'}, (err, macAddress) ->
return done(err) if err
expect(err).to.be.null
expect(macAddress).to.eql('00:18:31:8A:41:C6')
return done()

describe 'preset ifconfig', (describe,it) ->
data = """
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
Expand Down Expand Up @@ -82,6 +89,13 @@ joe.describe 'getmac', (describe,it) ->
expect(macAddress).to.eql('b8:8d:12:07:6b:ac')
return done()

it 'got the mac address of p2p0 successfully', (done) ->
getMac {data, iface: 'p2p0'}, (err, macAddress) ->
return done(err) if err
expect(err).to.be.null
expect(macAddress).to.eql('0a:8d:12:07:6a:bc')
return done()

describe 'preset ip link', (describe,it) ->
data = """
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
Expand All @@ -101,6 +115,13 @@ joe.describe 'getmac', (describe,it) ->
expect(macAddress).to.eql('bc:76:4e:20:7d:dd')
return done()

it 'got the mac address of eth1 successfully', (done) ->
getMac {data, iface: 'eth1'}, (err, macAddress) ->
return done(err) if err
expect(err).to.be.null
expect(macAddress).to.eql('bc:76:4e:20:99:be')
return done()

describe 'system', (describe,it) ->
it 'got the default mac address successfully', (done) ->
getMac (err, macAddress) ->
Expand Down