hostile v1.4.0
hostile

Simple, programmatic /etc/hosts manipulation (in node.js)

install
npm install hostileusage
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.
list all host file records
hostile listset a domain in the hosts file
hostile set [ip] [host]examples:
hostile set localhost domain.com
hostile set 192.168.33.10 domain.comremove a domain from the hosts file
hostile remove [host]example:
hostile remove domain.comload a set of hosts from a file
hostile load [file_path]hosts.txt
# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.comexample:
hostile load hosts.txtunload remove a set of hosts from a file
hostile unload [file_path]# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.comexample:
hostile unload hosts.txtmethods
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.
add a rule to /etc/hosts
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.
remove a rule from /etc/hosts
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.
get all lines in /etc/hosts
// 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]
})
})get all lines in any file
// 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]
})
})contributors
- Feross Aboukhadijeh (author)
- Maayan Glikser
license
MIT. Copyright (c) Feross Aboukhadijeh.
2 years ago
5 years ago
7 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago