Fork of https://github.com/feross/hostile
ES6
Makes a backup of hosts
file each time it writes something.
- unmaintained, last release on May 2018
- after it emptied my beloved
hosts
and I couldn't recoverit it I found out that the backup is out of scope feross/hostile#20 - Some other extra features and changes
npm install apikay/hostler
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:
hostler list [all]
all
is optional and lists all lines
hostler set [ip] [host] [comment]
comment
is optional and ads a comment to that line
examples:
hostler set localhost domain.com
hostler set 192.168.33.10 domain.com "what a wonderful world"
hostler remove [host]
example:
hostler remove domain.com
hostler load [file_path]
hosts.txt
# hosts.txt
127.0.0.1 github.com # git repos babe
127.0.0.1 twitter.com
example:
hostler load hosts.txt
hostler unload [file_path]
# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.com
example:
hostler unload hosts.txt
(not tested)
bash:
hostler --completion >> ~/hostler.completion.sh
echo 'source ~/hostler.completion.sh' >> .bash_profile
zsh:
echo '. <(./hostler --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 hostler = require('hostler')
hostler.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.
hostler.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
hostler.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
hostler.getFile(file_path, preserveFormatting, function (err, lines) {
if (err) {
console.error(err.message)
}
lines.forEach(function (line) {
console.log(line) // [IP, Host]
})
})
Original contributors:
- Feross Aboukhadijeh (author)
- Maayan Glikser