npm install hostile
Put the entries you want to add/remove into files under the "sets" folder. Run the following commands to give sudo access to the remainder of the terminal session and start up the hosts file manager:
sudo -s
bin/cmd.js i
You will be presented a list of host file entry sets. You may select or deselect one or more sets with <space>
, then press <enter>
to make the change to the hosts file. Press <ctrl-c>
to exit.
If you use OS X or Linux, this module assumes your hosts file is at /etc/hosts
. On
Windows, it assumes your hosts file is at C:/Windows/System32/drivers/etc/hosts
.
Commands that modify the hosts file require root privileges.
hostile list
hostile set [ip] [host]
examples:
hostile set localhost domain.com
hostile set 192.168.33.10 domain.com
hostile remove [host]
example:
hostile remove domain.com
hostile load [file_path]
hosts.txt
# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.com
example:
hostile load hosts.txt
hostile unload [file_path]
# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.com
example:
hostile unload hosts.txt
bash:
hostile --completion >> ~/hostile.completion.sh
echo 'source ~/hostile.completion.sh' >> .bash_profile
zsh:
echo '. <(./hostile --completion)' >> .zshrc
Commands that modify the hosts file require root privileges.
I wouldn't recommend running your production node server with admin privileges unless you
downgrade to a normal user with
process.setuid(id)
before you start accepting requests.
All methods have sync versions. Just omit the callback parameter.
var hostile = require('hostile')
hostile.set('127.0.0.1', 'peercdn.com', function (err) {
if (err) {
console.error(err)
} else {
console.log('set /etc/hosts successfully!')
}
})
If the rule already exists, then this does nothing.
hostile.remove('127.0.0.1', 'peercdn.com', function (err) {
if (err) {
console.error(err)
} else {
console.log('set /etc/hosts successfully!')
}
})
If the rule does not exist, then this does nothing.
// If `preserveFormatting` is true, then include comments, blank lines and other
// non-host entries in the result
var preserveFormatting = false
hostile.get(preserveFormatting, function (err, lines) {
if (err) {
console.error(err.message)
}
lines.forEach(function (line) {
console.log(line) // [IP, Host]
})
})
// If `preserveFormatting` is true, then include comments, blank lines and other
// non-host entries in the result
var preserveFormatting = false
hostile.getFile(file_path, preserveFormatting, function (err, lines) {
if (err) {
console.error(err.message)
}
lines.forEach(function (line) {
console.log(line) // [IP, Host]
})
})
- Feross Aboukhadijeh (author)
- Maayan Glikser
MIT. Copyright (c) Feross Aboukhadijeh.