1.4.0 • Published 3 months ago

hostile v1.4.0

Weekly downloads
46,938
License
MIT
Repository
github
Last release
3 months ago

hostile ci 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

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.

@telusdigital/shippy-cli@rafaelcalpena/hostile-promisesfokcusldresfokcus-devmode@45air/air-local-dockernitin-shippy-clicli-shippywxou@easystack-fe/switch-proxy-target@infinitebrahmanuniverse/nolb-hos@everything-registry/sub-chunk-1861@cannery/hoistseaport-advertxpm-toolsvpn-splittotvs-dtsenv-cliwebpack-launcher-utilswp-local-dockerwp-local-docker-generatorxxxtentacionydkweb-config10web2container4devwomplib.jsworkfytuatuaxsimple-host-proxyshiashieldfy-nodejs-clientsnark-cliswitch-proxy-targettangerinetapc-plugin-monitorsonic-serverstormragespandxspandx-crcstacker-coresgscskusku-for-testingsprucebot-clirebird-https-letsencrypt@code_monk/hak-cli@code_monk/hak-reverseproxy@code_monk/reverseproxy@csssr/codeceptjs@csssr/testcafe-browser-provider-csssr@idearium/clicustom-local-domain@frag-crypto/frag-core@freelog/freelog-dev-server@garbados/dat-boi@fork-hu/cli@3share/3share-clidocker-discover-tasksdockerappmanagercreate-wp-site@goldencomm/build-scripts@jumpgroup/trellis-tools@jzetlen/pwa-buildpack@mockswitch/cli@mongodb-js/devtools-docker-test-envs@nlv8/build-systemaccessproxyffocusQAP-clia1media-concorda1media-concord2flat101-utilsfast-hostsenkel_cliemergencemongo-dns-hack@wecode/wlk-cli@weidian-inc/wdy-cli-coremockmkube-mirrorlarawin@unumux/update-hosts-ip@ufly/sameh-hostsmake-proxyapisailor-cliapisailor-serverapeman-task-hostsexpress-https-providerexframe-integrationloading-infop57dockerorigin-mint-bot@telus/shippy-cliletswork@rocketmakers/shell-commands@sprucelabs/sprucebot-cli@sliomere/jeeveskitchen-toolslocal.dev
1.4.0

3 months ago

1.3.3

3 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

8 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.7.0

9 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.2

10 years ago

0.1.1

11 years ago

0.1.0

11 years ago