Created
February 12, 2020 17:03
-
-
Save agconti/f5014a6a8919f0c84f71b78868e83227 to your computer and use it in GitHub Desktop.
A script for pragmatically setting `/ect/hosts` with node.js
This file contains hidden or 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
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