Skip to content

Instantly share code, notes, and snippets.

@reidstidolph
Last active November 24, 2018 18:30
Show Gist options
  • Save reidstidolph/6e9f6eefb2291c8714a52693bca8c169 to your computer and use it in GitHub Desktop.
Save reidstidolph/6e9f6eefb2291c8714a52693bca8c169 to your computer and use it in GitHub Desktop.
Looks for a tenant-devices.json file in it's current directory, and uses it to produce a bit of config for dhcpd. See code comment block for expected json object.
#!/usr/bin/env node
'use strict';
/*
Parses tenant-devices.json file. For each tenant and client named in the array
in the form of:
[
"tenantName": "my-tenant",
"clients": [
{
"name": "my-client-name",
"mac" : "00:11:22:33:44:55"
}
]
]
...it will produce an entry in a clients config file in the form of:
host <name> {
option dhcp-client-identifier 1:<mac>;
hardware ethernet <mac>;
option host-name "<name>.<domain>";
}
subclass "<tenant>" 1:<mac>;
subclass "<tenant>" <mac>;
*/
const fs = require('fs')
const domain = "my.domain.com"
const outputFile = "dhcpd-clients.conf"
var outputString = ""
try {
var tenants = require("./tenant-devices.json")
} catch (e) {
console.log(e);
process.stdout.write(`\n\nCould not process 'tenant-devices.json' file.\n\n`);
process.exit(1);
}
tenants.forEach((tenant)=>{
tenant.clients.forEach((client)=>{
outputString += `host ${client.name} {
option dhcp-client-identifier 1:${client.mac};
hardware ethernet ${client.mac};
option host-name "${client.name}.${domain}";
}
subclass "${tenant.tenantName}" 1:${client.mac};
subclass "${tenant.tenantName}" ${client.mac};
`
})
})
fs.writeFile(outputFile, outputString, (err)=>{
if (err) console.log(err);
console.log(`Successfully Written to ${outputFile}.`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment