Technology moves fast! ⚡ Don't get left behind.🚶 Subscribe to our mailing list to keep up with latest and greatest in open source projects! 🏆


Subscribe to our mailing list

hostile

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

Subscribe to updates I use hostile


Statistics on hostile

Number of watchers on Github 454
Number of open issues 2
Average time to close an issue about 1 month
Main language JavaScript
Average time to merge a PR 2 days
Open pull requests 3+
Closed pull requests 3+
Last commit over 1 year ago
Repo Created about 6 years ago
Repo Last Updated over 1 year ago
Size 75 KB
Organization / Authorfeross
Contributors6
Page Updated
Do you use hostile? Leave a review!
View open issues (2)
View hostile activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating hostile for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

hostile travis npm downloads javascript style guide

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

hostile

install

npm install hostile

usage

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 list

set a domain in the hosts file

hostile set [ip] [host]

examples:

hostile set localhost domain.com
hostile set 192.168.33.10 domain.com

remove a domain from the hosts file

hostile remove [host]

example:

hostile remove domain.com

load 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.com

example:

hostile load hosts.txt

unload [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.com

example:

hostile unload hosts.txt

set up auto completion

bash:

hostile --completion >> ~/hostile.completion.sh
echo 'source ~/hostile.completion.sh' >> .bash_profile

zsh:

echo '. <(./hostile --completion)' >> .zshrc

methods

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

license

MIT. Copyright (c) Feross Aboukhadijeh.

hostile open pull requests (View All Pulls)
  • Add the ability to load/unload hosts from a file
  • Ignore in-line comments
  • Remove trailing whitespace from parsed domains
hostile questions on Stackoverflow (View All Questions)
  • Treating form data as hostile and validating securely
  • How to avoid creating Thread hostile classes in Java
  • Socket.io Rooms in a Hostile Network Environment?
  • Is ostream operator<< in libstdc++ thread-hostile?
  • Running the OSGi Equinox console on an hostile environment
  • Protecting a system deployed in a "hostile" environment
  • Stopping Hostile Threads in JVM
  • Tips for coding using hostile tools?
  • How can I secure memcached/beanstalkd in a hostile cloud environment?
  • Is there a not user-hostile tool for making/editing vimrc files?
  • Identifying hostile web crawlers
  • What is the most hostile corporate environment to deploy a .NET WinForms app on?
  • Developing in a hostile environment
hostile list of languages used
Other projects in JavaScript