Hosty

Hosty is a command-line utility that allows for fast inspection and editing of /etc/hosts-like files. It is written in golang and uses libhosty as the underlying library to operate on files

Main Features

  • Fast Add/Delete/Comment/Uncomment entries
  • JSON output for easy parsing
  • Backup files before editing

Installation

Precompiled packages

You can install hosty with one of the precompiled packages in the release section

Build

Ensure you have go on your system

[email protected]$ go version
go version go1.16.3 linux/amd64

pull the app

[email protected]$ go get github.com/areYouLazy/hosty

build and install

[email protected]$ cd github.com/areYouLazy/hosty
[email protected]$ go build
[email protected]$ go install

You should now be able to use hosty. Remember that Add/Delete/Comment/Uncomment commands needs root privileges.

[email protected]$ hosty show localhost
127.0.0.1       localhost
::1             localhost

[email protected]$ hosty comment 127.0.0.1
done

Usage

[email protected]$ hosty -h
Hosty is a command-line tool to interact with the /etc/hosts file.
It allows for fast inspect and edit of the file. Main goals of this tool are to be fast, reliable and scriptable.
Hosty uses libhosty to manipulate the file. You can find more about libhosty at https://github.com/areYouLazy/libhosty

Usage:
  hosty [command]

Available Commands:
  add         Add file data
  comment     Comment file data
  delete      Delete file data
  export      Export file to a custom location
  help        Help about any command
  restore     Restore default hosts file
  show        Show file data
  uncomment   Uncomment file data

Flags:
  -b, --backup        backup file before editing. Backup is hidden and named with the format: .YYYYMMDDHHmmss-hosts.bck
  -f, --file string   parse a custom /etc/hosts-like file instead of the system default one
  -h, --help          help for hosty
  -j, --json          print output in json format for easy parsing
  -q, --quiet         suppress every output except for errors
  -v, --version       version for hosty

Use "hosty [command] --help" for more information about a command.

Examples

[email protected]$ # show entries with hostname equale to localhost
[email protected]$ hosty show localhost
127.0.0.1         localhost
::1               localhost

[email protected]$ # show entries with ip equal to 127.0.0.1
[email protected]$ hosty show 127.0.0.1
127.0.0.1         localhost

[email protected]$ # add an entrie with ip 1.2.3.4 and hostname my.custom.dns
[email protected]$ hosty add 1.2.3.4 my.custom.dns
done

[email protected]$ # show entries with ip equal to 1.2.3.4
[email protected]$ hosty show 1.2.3.4
1.2.3.4         my.custom.dns

[email protected]$ # comment entries with ip equal to 1.2.3.4
[email protected]$ hosty comment 1.2.3.4
done

[email protected]$ # show entries with ip equal to 1.2.3.4
[email protected]$ hosty show 1.2.3.4
# 1.2.3.4         my.custom.dns

[email protected]$ # uncomment entries with ip equal to 1.2.3.4 suppress output
[email protected]$ hosty uncomment 1.2.3.4 --quiet

[email protected]$ # show entries with ip equal to 1.2.3.4 output in json
[email protected]$ hosty show 1.2.3.4 --json | jq .
{
  "raw": "1.2.3.4         my.custom.dns"
}

[email protected]$ # add another entry with same ip and different hostname output in json
[email protected]$ hosty add 1.2.3.4 my.custom2.dns --json | jq .
{
  "done": true
}

[email protected]$ # show entries with ip equal to 1.2.3.4 output in json with details
[email protected]$ hosty show 1.2.3.4 --json --details | jq .
{
  "action": "show",
  "number": 12,
  "type": "address",
  "address": "1.2.3.4",
  "hostnames": [
    "my.custom.dns",
    "my.custom2.dns"
  ],
  "comment": "",
  "is_commented": false,
  "raw": "1.2.3.4       my.custom.dns my.custom2.dns"
}

[email protected]$ # delete entries with ip equal to 1.2.3.4
[email protected]$ hosty delete 1.2.3.4
done

[email protected]$ # show entries with ip equal to 1.2.3.4
[email protected]$ hosty show 1.2.3.4
nothing found for ip 1.2.3.4

Commands and Aliases

Here's a little explanation of every command.

Every Hosty command supports several aliases, most of which are just truncated version of the command.

show

[email protected]$ hosty show [PARAMETER]

Show entries based on the given parameter.
Parameter can be both IP or FQDN.
A call to hosty show without arguments will dump every entry in the file

[email protected]$ # Aliases
[email protected]$ hosty sho localhost
[email protected]$ hosty sh localhost
[email protected]$ hosty s localhost

add

[email protected]$ hosty add [IP] [FQDN] [COMMENT]

Add an entry to hosts file.
Required parameters are IP and FQDN, optionally you can pass a comment for the line.

[email protected]$ # Aliases
[email protected]$ hosty ad 1.2.3.4 my.custom.dns "DNS#1"
[email protected]$ hosty a 1.2.3.4 my.custom.dns

delete

[email protected]$ hosty delete [PARAMETER]

Delete entries from hosts file based on the given parameter.
Parameter can be both IP or FQDN.

[email protected]$ # Aliases
[email protected]$ hosty delet 1.2.3.4
[email protected]$ hosty dele 1.2.3.4
[email protected]$ hosty del 1.2.3.4
[email protected]$ hosty de 1.2.3.4
[email protected]$ hosty d 1.2.3.4
[email protected]$ hosty rem 1.2.3.4
[email protected]$ hosty rm 1.2.3.4

comment

[email protected]$ hosty comment [PARAMETER]

Comment entries on hosts file based on given parameter.
Parameter can be both IP or FQDN.

[email protected]$ # Aliases
[email protected]$ hosty commen 1.2.3.4
[email protected]$ hosty comme 1.2.3.4
[email protected]$ hosty comm 1.2.3.4
[email protected]$ hosty com 1.2.3.4
[email protected]$ hosty co 1.2.3.4
[email protected]$ hosty c 1.2.3.4

uncomment

[email protected]$ hosty uncomment [PARAMETER]

Uncomment entries on hosts file based on given parameter.
Parameter can be both IP or FQDN.

[email protected]$ # Aliases
[email protected]$ hosty uncommen 1.2.3.4
[email protected]$ hosty uncomme 1.2.3.4
[email protected]$ hosty uncomm 1.2.3.4
[email protected]$ hosty uncom 1.2.3.4
[email protected]$ hosty unco 1.2.3.4
[email protected]$ hosty unc 1.2.3.4
[email protected]$ hosty un 1.2.3.4
[email protected]$ hosty u 1.2.3.4

export

[email protected]$ hosty export [PARAMETER]

Export file content to a given location
Parameter must be a writable file

[email protected]$ # Aliases
[email protected]$ hosty expor /home/sonica/hosts-export.txt
[email protected]$ hosty expo /home/sonica/hosts-export.txt
[email protected]$ hosty exp /home/sonica/hosts-export.txt
[email protected]$ hosty ex /home/sonica/hosts-export.txt
[email protected]$ hosty e /home/sonica/hosts-export.txt

restore

[email protected]$ hosty restore [PARAMETER]

Restore default hosts file for given OS
If Paramter is omitted, hosty will try to guess the OS and restore the appropriate file

[email protected]$ # Aliases
r[email protected]$ hosty restor darwin
[email protected]$ hosty resto darwin
[email protected]$ hosty rest darwin
[email protected]$ hosty res darwin
[email protected]$ hosty re darwin
[email protected]$ hosty r darwin

License

Released under Apache 2.0 license

GitHub

https://github.com/areYouLazy/hosty