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

Overview

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

[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]$ hostyc-li 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
{"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": "127.0.0.1       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.

[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
[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

Issues
  • 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

    opened by c-nv-s 10
  • 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.

    opened by c-nv-s 4
  • 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
    
    opened by areYouLazy 1
  • 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

    [email protected]$ 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

    [email protected]$ hosty show 1.2.3.4
    1.2.3.4    my.tabbed.host
    

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

    bug help wanted 
    opened by areYouLazy 0
Releases(v1.1.1)
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

Geet Sethi 1 Mar 27, 2022
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

Shoki Hata 4 Jun 25, 2022
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

Souvik 157 Aug 3, 2022
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.

Adam Tauber 10k Aug 12, 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

Kristof Kowalski 8 Jul 25, 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

null 0 Nov 22, 2021
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

Clement Sam 2k Aug 9, 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

Venky 0 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.

Abhimanyu Sharma 60 Sep 21, 2021
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

Avi Zimmerman 11 Nov 14, 2021
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 ??️ ?? ??

Zino Kader 183 Aug 11, 2022
A command line utility for labeling GitHub issues and pull requests

A command line utility for labeling GitHub issues and pull requests

Jim Schubert 3 Nov 12, 2021
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

null 0 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

Andrej Benz 3 Apr 16, 2022
Libraries and CLIs for my personal all-in-one productivity system including components like bookmarks, notes, todos, projects, etc.

bntp.go Libraries and CLIs for my personal all-in-one productivity system including components like bookmarks, notes, todos, projects, etc. Neovim int

Jonas Mühlmann 13 Aug 10, 2022
📷 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

Gregory Schier 21 May 31, 2022
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

Shravan 18 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

Shayan 53 Jul 26, 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

Ishan Goel 11 Aug 10, 2022