Skip to content

Instantly share code, notes, and snippets.

@agconti
Created Feb 12, 2020
Embed
What would you like to do?
A script for pragmatically setting `/ect/hosts` with node.js
const { promisify } = require('util')
const { promises: fs } = require('fs')
const hostile = require('hostile')
const setHost = promisify(hostile.set)
const removeHost = promisify(hostile.remove)
const LOCALHOST = '127.0.0.1'
const hosts = [
[LOCALHOST, 'youralias'],
]
const apply = async (func, hosts) => {
for(const [source, alias] of hosts) {
await func(source, alias)
}
}
const set = async () => apply(setHost, hosts)
const remove = async () => apply(removeHost, hosts)
const main = async () => {
await remove()
const data = await fs.readFile('/etc/hosts', { encoding: 'utf8' })
process.stdout.write(data)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment