Code Monkey home page Code Monkey logo

hostile's Introduction

hostile travis npm gittip

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's People

Contributors

feross avatar jhermsmeier avatar morriswchris avatar morsdyce avatar xiaojue avatar yorkie avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.