Skip to content

Instantly share code, notes, and snippets.

@agconti
Created February 12, 2020 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agconti/f5014a6a8919f0c84f71b78868e83227 to your computer and use it in GitHub Desktop.
Save agconti/f5014a6a8919f0c84f71b78868e83227 to your computer and use it in GitHub Desktop.
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