host-switch

Created: 2014-05-19 04:53
Updated: 2016-03-26 14:46

README.md

host-switch

A CLI tool to easily switch between different hosts settings

Commands

$ host-switch help
Usage:
    host-switch                     show current setting
    host-switch list <set>          show current setting of specific set
    host-switch dig <set>           show current host table of specific set
    host-switch all-off <set>       (root) turn off all local settings (Use DNS)
    host-switch on <set> <group>    (root) to switch <group> hosts setting ON

Definition

  • GROUP: indicate one specific setting of some hostnames
  • SET: indicate an aggregation of hosts. You can define several setting group for one set, but only one group setting can be activated simultaneously.

Usage

  1. For example, I have two host-set in my daily development, say foo and bar.

  2. So I prepare my /etc/hosts file as the following formats:

     # # begin foo:DEV-my host setting
     # # put any comment / description here if you want
     # 10.1.0.35 my.foo.com
     # 10.1.0.36 my2.foo.com
     # # end foo:DEV-my host setting
    
     # # begin foo:STAGING host setting
     # 10.3.0.47 my.foo.com
     # 10.3.0.48 my2.foo.com
     # # end foo:STAGING host setting
    
     # # begin bar:QA host setting
     # 10.2.0.100 my.bar.net
     # 10.2.0.110 my2.bar.net
     # # end bar:QA host setting
    
     # # begin bar:testing host setting
     # 10.2.10.33 my.bar.net
     # 10.2.10.39 my2.bar.net
     # # end bar:testing host setting
    

    Note: host setting name (case-sesitive, which are DEV-my and STAGING in the above case) should match on begin and end line.

  3. Then I can use CLI to manage my hosts table switching:

    Show current setting

     $ host-switch
    

    Show current setting for foo set

     $ host-switch list set
    

    Turn on DEV-my host setting for foo set

     $ sudo host-switch on foo DEV-my
    

    Turn on STAGING host setting (and implicitly turn off other exists setting)

     $ sudo host-switch on foo STAGING
    

    Turn off all local host setting (will use global DNS lookup)

     $ sudo host-switch all-off foo
    

    Are you lazy to type password for sudo? Then you can use the shell feature. Behind the shell prompt, all the commands are the same as the CLI mode, but no need to type the script name (i.e., host-switch should be stripped).

     $ sudo host-switch shell
    

License

FreeBSD License

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more