Skip to content
/ hostler Public

Programmatic `/etc/hosts` manipulation (in node.js)

License

Notifications You must be signed in to change notification settings

apikay/hostler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hostler

Programmatic /etc/hosts manipulation (in node.js)

Fork of https://github.com/feross/hostile

ES6

Makes a backup of hosts file each time it writes something.

Motivation

  • 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

install

npm install apikay/hostler

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

hostler list [all]

all is optional and lists all lines

- set a domain in the hosts file

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"

- remove a domain from the hosts file

hostler remove [host]

example:

hostler remove domain.com

- load a set of hosts from a file

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

- unload [remove] a set of hosts from a file

hostler unload [file_path]
# hosts.txt
127.0.0.1 github.com
127.0.0.1 twitter.com

example:

hostler unload hosts.txt

- set up auto completion

(not tested)

bash:

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

zsh:

echo '. <(./hostler --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 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.

remove a rule from /etc/hosts

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.

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

hostler.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

hostler.getFile(file_path, preserveFormatting, function (err, lines) {
  if (err) {
    console.error(err.message)
  }
  lines.forEach(function (line) {
    console.log(line) // [IP, Host]
  })
})

contributors

Original contributors:

license: MIT.

About

Programmatic `/etc/hosts` manipulation (in node.js)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published