Hosty is a command-line utility that allows for fast inspection and editing of /etc/hosts-like files

Hosty

Description

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

Table of Content

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

> go version
go version go1.16.3 linux/amd64

pull the app

> go get github.com/areYouLazy/hosty

build and install

> cd github.com/areYouLazy/hosty
> go build
> go install

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

> hosty show localhost
127.0.0.1       localhost
::1             localhost

> hosty comment 127.0.0.1
done

Usage

root@localhost$ 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

root@localhost$ # show entries with hostname equale to localhost
root@localhost$ hosty show localhost
127.0.0.1         localhost
::1               localhost

root@localhost$ # show entries with ip equal to 127.0.0.1
root@localhost$ hosty show 127.0.0.1
127.0.0.1         localhost

root@localhost$ # add an entrie with ip 1.2.3.4 and hostname my.custom.dns
root@localhost$ hosty add 1.2.3.4 my.custom.dns
done

root@localhost$ # show entries with ip equal to 1.2.3.4
root@localhost$ hosty show 1.2.3.4
1.2.3.4         my.custom.dns

root@localhost$ # comment entries with ip equal to 1.2.3.4
root@localhost$ hosty comment 1.2.3.4
done

root@localhost$ # show entries with ip equal to 1.2.3.4
root@localhost$ hostyc-li show 1.2.3.4
# 1.2.3.4         my.custom.dns

root@localhost$ # uncomment entries with ip equal to 1.2.3.4 suppress output
root@localhost$ hosty uncomment 1.2.3.4 --quiet

root@localhost$ # show entries with ip equal to 1.2.3.4 output in json
root@localhost$ hosty show 1.2.3.4 --json | jq .
{
  "raw": "1.2.3.4         my.custom.dns"
}

root@localhost$ # add another entry with same ip and different hostname output in json
root@localhost$ hosty add 1.2.3.4 my.custom2.dns --json
{"done":true}

root@localhost$ # show entries with ip equal to 1.2.3.4 output in json with details
root@localhost$ 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": "127.0.0.1       my.custom.dns my.custom2.dns"
}

root@localhost$ # delete entries with ip equal to 1.2.3.4
root@localhost$ hosty delete 1.2.3.4
done

root@localhost$ # show entries with ip equal to 1.2.3.4
root@localhost$ 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

root@localhost$ hosty show [PARAMETER]

Show entries based on the given parameter. Parameter can be both IP or FQDN.

root@localhost$ # Aliases
root@localhost$ hosty sho localhost
root@localhost$ hosty sh localhost
root@localhost$ hosty s localhost

add

root@localhost$ 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.

root@localhost$ # Aliases
root@localhost$ hosty ad 1.2.3.4 my.custom.dns "DNS#1"
root@localhost$ hosty a 1.2.3.4 my.custom.dns

delete

root@localhost$ hosty delete [PARAMETER]

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

root@localhost$ # Aliases
root@localhost$ hosty delet 1.2.3.4
root@localhost$ hosty dele 1.2.3.4
root@localhost$ hosty del 1.2.3.4
root@localhost$ hosty de 1.2.3.4
root@localhost$ hosty d 1.2.3.4
root@localhost$ hosty rem 1.2.3.4
root@localhost$ hosty rm 1.2.3.4

comment

root@localhost$ hosty comment [PARAMETER]

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

root@localhost$ # Aliases
root@localhost$ hosty commen 1.2.3.4
root@localhost$ hosty comme 1.2.3.4
root@localhost$ hosty comm 1.2.3.4
root@localhost$ hosty com 1.2.3.4
root@localhost$ hosty co 1.2.3.4
root@localhost$ hosty c 1.2.3.4

uncomment

root@localhost$ hosty uncomment [PARAMETER]

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

root@localhost$ # Aliases
root@localhost$ hosty uncommen 1.2.3.4
root@localhost$ hosty uncomme 1.2.3.4
root@localhost$ hosty uncomm 1.2.3.4
root@localhost$ hosty uncom 1.2.3.4
root@localhost$ hosty unco 1.2.3.4
root@localhost$ hosty unc 1.2.3.4
root@localhost$ hosty un 1.2.3.4
root@localhost$ hosty u 1.2.3.4

export

root@localhost$ hosty export [PARAMETER]

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

root@localhost$ # Aliases
root@localhost$ hosty expor /home/sonica/hosts-export.txt
root@localhost$ hosty expo /home/sonica/hosts-export.txt
root@localhost$ hosty exp /home/sonica/hosts-export.txt
root@localhost$ hosty ex /home/sonica/hosts-export.txt
root@localhost$ hosty e /home/sonica/hosts-export.txt

restore

root@localhost$ 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

root@localhost$ # Aliases
root@localhost$ hosty restor darwin
root@localhost$ hosty resto darwin
root@localhost$ hosty rest darwin
root@localhost$ hosty res darwin
root@localhost$ hosty re darwin
root@localhost$ hosty r darwin

License

Released under Apache 2.0 license

Similar Resources

Portal is a quick and easy command-line file transfer utility from any computer to another πŸ–₯️ 🌌 πŸ’»

Portal is a quick and easy command-line file transfer utility from any computer to another πŸ–₯️ 🌌 πŸ’»

Portal is a quick and easy command-line file transfer utility from any computer to another πŸ–₯️ 🌌 πŸ’»

Dec 27, 2022

A command line utility for labeling GitHub issues and pull requests

A command line utility for labeling GitHub issues and pull requests

Jan 8, 2023

Gopassutil - Command line utility to hash and verify with passlib for go

gopassutil command line utility to hash and verify with passlib for go Usage Gen

Feb 1, 2022

Related is a simple cli utility tool to create files or a group of files.

Related - Create files based on individual definitions or groups Related helps with common file-creation-based tasks. You can predefine single types a

Apr 16, 2022

πŸ“· Command-line utility to download all photos from Instagram

πŸ“· Command-line utility to download all photos from Instagram

Instagram Downloader This is a simple command-line tool, written in Go, to download all images from an Instagram account. Getting Started Install inst

Sep 9, 2022

A command line utility for generating language-specific project structure.

A command line utility for generating language-specific project structure.

hydra hydra is a command line utility for generating language-specific project structures. ⏬ ✨ Features Build project templates with just one command

Oct 8, 2021

A small utility command line application that can recursively download Notion pages

notionbackup A small utility command line application that can recursively download Notion pages. I needed something scriptable that could periodicall

Dec 5, 2022

Aces is a command line utility that lets you encode any file to a character set of your choice.

Aces Any Character Encoding Set Aces is a command line utility that lets you encode any file to a character set of your choice. For example, you could

Nov 28, 2022

πŸ“ˆ A command-line utility to interact with TradingView

tvctl πŸ“ˆ A command-line utility to interact with TradingView. This utility aims to improve usability of tedius and repetative tasks, such as watchlist

Oct 18, 2022
Comments
  • display all entries - hosty show command without parameters

    display all entries - hosty show command without parameters

    Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

    it would be nice to be able to just type "hosty show" (without parameters) and receive all the current host file entries or type "hosty show --json" and receive all the current entries in json format

  • feature request: recognize tab character

    feature request: recognize tab character

    Is your feature request related to a problem? Please describe. At present if your hosts file only contains tab delimiters then the output of hosty's commands do not preserve the spacing and thus you receive a concatanated ip address with the hostname in the result.

    Describe the solution you'd like It would be nice for hosty to respect/preserve tab characters or replace them with a space when displaying results

    Describe alternatives you've considered Alternative is to force everyone to only use space separated entries in their hosts files.

  • panic when removing line by hostname

    panic when removing line by hostname

    Describe the bug When removing a line using hostname, libhosty panic

    To Reproduce Steps to reproduce the behavior:

    1. Add a new line with hosty
    2. Delete that same line by hostname with hosty

    Expected behavior Line should be removed without errors

    Screenshots

    $ sudo hosty add 123.123.123.123 delta.dns
    done
    $ sudo hosty d delta
    panic: runtime error: index out of range [13] with length 13
    
    goroutine 1 [running]:
    github.com/areYouLazy/libhosty.(*HostsFile).RemoveHostsFileLinesByHostnameAsRegexp(0xc000012b40, 0x0, 0x0)
    	/home/emil/go/pkg/mod/github.com/are!you!lazy/[email protected]/libhosty.go:392 +0x3da
    github.com/areYouLazy/hosty/cmd.glob..func4(0x729e40, 0xc00005c7c0, 0x1, 0x1)
    	/home/emil/go/src/github.com/areYouLazy/hosty/cmd/delete.go:40 +0x3d2
    github.com/spf13/cobra.(*Command).execute(0x729e40, 0xc00005c7a0, 0x1, 0x1, 0x729e40, 0xc00005c7a0)
    	/home/emil/go/pkg/mod/github.com/spf13/[email protected]/command.go:856 +0x2c2
    github.com/spf13/cobra.(*Command).ExecuteC(0x729440, 0x0, 0xffffffff, 0xc00007e058)
    	/home/emil/go/pkg/mod/github.com/spf13/[email protected]/command.go:960 +0x375
    github.com/spf13/cobra.(*Command).Execute(...)
    	/home/emil/go/pkg/mod/github.com/spf13/[email protected]/command.go:897
    github.com/areYouLazy/hosty/cmd.Execute(...)
    	/home/emil/go/src/github.com/areYouLazy/hosty/cmd/root.go:48
    main.main()
    	/home/emil/go/src/github.com/areYouLazy/hosty/main.go:8 +0x2e
    
  • Render tabs in CLI output

    Render tabs in CLI output

    Describe the bug As for now, tabbed lines are converted to spaces lines because the CLI output doesn't honor the tab chars

    root@localhost$ hosty show 1.2.3.4
    1.2.3.4\tmy.tabbed.host
    

    This does not affect file rendering

    To Reproduce

    1. Create a tabbed line with only 1 tab between IP and hostnames
    2. Show the new created line

    Expected behavior Tabbed line should be renderd correctly in CLI output also

    root@localhost$ hosty show 1.2.3.4
    1.2.3.4    my.tabbed.host
    

    Reference: https://github.com/areYouLazy/hosty/issues/1

Hasura-fzf - This command has a fzf-like UI that allows you to find and run the file version used by the hasura command

hasura-fzf This command has a fzf-like UI that allows you to find and run the fi

Sep 29, 2022
Watcher - A simple command line app to watch files in a directory for changes and run a command when files change!

Watcher - Develop your programs easily Watcher watches all the files present in the directory it is run from of the directory that is specified while

Mar 27, 2022
ls Xtended : A command line utility which lets you navigate through terminal like a pro 😎.
ls Xtended : A command line utility which lets you navigate through terminal like a pro 😎.

Navigate through terminal like a pro ?? ?? Demo β€’ βš—οΈ Installation β€’ ?? Contribution β€’ ❗ Known Issues ❓ Why? It's a pain to cd and ls multiple times to

Dec 14, 2022
πŸ”„ A command-line utility to export Protocol Buffers (proto) files to YAML, and JSON

proto2yaml ?? A command-line utility to export Protocol Buffers (proto) files to YAML, and JSON. Currently supported exports are for: Packages Service

Nov 10, 2022
Interactive cli tool for HTTP inspection
Interactive cli tool for HTTP inspection

Wuzz command line arguments are similar to cURL's arguments, so it can be used to inspect/modify requests copied from the browser's network inspector with the "copy as cURL" feature.

Dec 27, 2022
A command line tool that builds and (re)starts your web application everytime you save a Go or template fileA command line tool that builds and (re)starts your web application everytime you save a Go or template file

# Fresh Fresh is a command line tool that builds and (re)starts your web application everytime you save a Go or template file. If the web framework yo

Nov 22, 2021
An open-source GitLab command line tool bringing GitLab's cool features to your command line
An open-source GitLab command line tool bringing GitLab's cool features to your command line

GLab is an open source GitLab CLI tool bringing GitLab to your terminal next to where you are already working with git and your code without switching

Dec 30, 2022
A command line tool to prompt for a value to be included in another command line.

readval is a command line tool which is designed for one specific purposeβ€”to prompt for a value to be included in another command line. readval prints

Dec 22, 2021
sttr is command line software that allows you to quickly run various transformation operations on the string.
sttr is command line software that allows you to quickly run various transformation operations on the string.

sttr is command line software that allows you to quickly run various transformation operations on the string.

Sep 21, 2021
A command line utility and library for generating professional looking invoices in Go.
A command line utility and library for generating professional looking invoices in Go.

ginvoicer A command line utility and library for generating professional looking invoices in Go. This is a very rough draft and there could still be b

Dec 15, 2022