Simple hosts file management in Golang (deprecated).

Related tags

Network goodhosts
Overview

Goodhosts (deprecated)

This library is now deprecated. See the goodhosts organisation for the current maintained version.


Simple hosts file (/etc/hosts) management in Go (golang).


A Surrealist Parisian Dinner Party chez Madame Rothschild, 1972

Build Status

Features

  • List, add, remove and check hosts file entries from code or the command-line.
  • Windows support.

Command-Line Usage

Installation

Linux

Download the binary and put it in your path.

$ wget -O goodhosts https://github.com/lextoumbourou/goodhosts/releases/download/v2.1.0/goodhosts-linux
$ chmod +x goodhosts
$ export PATH=$(pwd):$PATH
$ goodhosts --help

Windows

Download the binary and do Windowsy stuff with it (doc PR welcome).

List entries

$ goodhosts list
127.0.0.1 localhost
10.0.0.5 my-home-server xbmc-server
10.0.0.6 my-desktop

Add --all flag to include comments.

Check for an entry

$ goodhosts check 127.0.0.1 facebook.com

Add an entry

$ goodhosts add 127.0.0.1 facebook.com

Or entries.

$ goodhosts add 127.0.0.1 facebook.com twitter.com gmail.com

Remove an entry

$ goodhosts rm 127.0.0.1 facebook.com

Or entries.

$ goodhosts rm 127.0.0.1 facebook.com twitter.com gmail.com

More

$ goodhosts --help

API Usage

Installation

$ go get github.com/lextoumbourou/goodhosts

List entries

package main

import (
    "fmt"
    "github.com/lextoumbourou/goodhosts"
)

func main() {
    hosts := goodhosts.NewHosts()

    for _, line := range hosts.Lines {
        fmt.Println(line.Raw)
    }
}

Check for an entry

package main

import (
    "fmt"
    "github.com/lextoumbourou/goodhosts"
)

func main() {
    hosts := goodhosts.NewHosts()

    if hosts.Has("127.0.0.1", "facebook.com") {
        fmt.Println("Entry exists!")
        return
    }

    fmt.Println("Entry doesn't exist!")
}

Add an entry

package main

import (
    "fmt"
    "github.com/lextoumbourou/goodhosts"
)

func main() {
    hosts := goodhosts.NewHosts()

    // Note that nothing will be added to the hosts file until ``hosts.Flush`` is called.
    hosts.Add("127.0.0.1", "facebook.com", "twitter.com")

    if err := hosts.Flush(); err != nil {
        panic(err)
    }
}

Remove an entry

package main

import (
    "fmt"
    "github.com/lextoumbourou/goodhosts"
)

func main() {
    hosts := goodhosts.NewHosts()

    // Same deal, yo: call hosts.Flush() to make permanent.
    hosts.Remove("127.0.0.1", "facebook.com", "twitter.com")

    if err := hosts.Flush(); err != nil {
        panic(err)
    }
}

More

Changelog

2.1.0 (2015-06-08)

  • Added Windows support.
  • Added command-line docs.

2.0.0 (2015-05-04)

  • Breaking API change.
  • Add support for adding and removing multiple hosts.
  • Added --all flag.
  • Handle malformed IP addresses.

1.0.0 (2015-05-03)

  • Initial release.

License

MIT


Comments
  • All hosts on one line causes resolution trouble, especially on Windows

    All hosts on one line causes resolution trouble, especially on Windows

    Could goodhosts use a single line per host in the hosts file? Or a limit of how many hosts get added to a single line? The long lines seem to cause occasional trouble with name resolution macOS, and more than 10 hosts per line seems to be completely nonfunctional at least in some WIndows versions, see https://superuser.com/questions/932112/is-there-a-maxium-number-of-hostname-aliases-per-line-in-a-windows-hosts-file

    Spurred by request in https://github.com/drud/ddev/issues/948

    opened by rfay 7
  • Disable

    Disable "appendToLine" behaviour on Windows due to limit on hostnames per line

    On Windows there is a limitation on the number of hostnames per line (hostnames over this limit are simply ignored), but there is no such limit as long as you add each mapping on an individual line.

    This pull request introduces appendToLine constant, which is set based on OS targeted - disabling the behaviour of appending to existing lines when compiled for Windows.

    I have also updates the test cases to test in both scenarios (which is the reason why the constant is declared as var so the test case can change it).

    opened by micdah 5
  • Add the function of remove items by hostname.

    Add the function of remove items by hostname.

    Use exist funcs to implement 'remove items by hostname' function.

    Checking hostname in Hosts array by strings.Contains in strings packs.

    func sliceContainsItem(item string, list []string) bool {
    	for _, i := range list {
    		if strings.Contains(i, item) {
    			return true
    		}
    	}
    
    	return false
    }
    

    Please review my code and decide to merge. Thank you!

    opened by edwardstudy 2
  • Linux release of 2.1 is actually macOS

    Linux release of 2.1 is actually macOS

    The Linux download for version 2.1.0 of this program isn't actually a linux executable, but instead a Mach-O 64-bit executable.

    Not only that, but the executable as it stands doesn't even work on macOS High Sierra - I get a runtime error instead.

    failed MSpanList_Insert 0x279000 0x26611aa86d8 0x0
    fatal error: MSpanList_Insert
    

    I think that the version of Go used to compile the program in the release is too out-of-date to work on High Sierra.

    opened by AlexMax 1
  • add support for custom hosts location via an environment variable

    add support for custom hosts location via an environment variable

    This is a change that allows for custom hosts locations to be passed in via an environment variable HOSTS_PATH.

    By having that capability the library/tool will compile and run in custom chroot/jails. All tests etc remain the same.

    opened by jimmystewpot 0
  • Add support for using a custom hosts file path.

    Add support for using a custom hosts file path.

    This adds support for being able to manage a hosts file in a non-default path, especially useful when leveraging a service like dnsmasq to include a hosts file that is exclusive to the system's default /etc/hosts:

    # cat /etc/NetworkManager/dnsmasq.d/cluster.conf 
    listen-address=::1
    cache-size=1000
    no-negcache
    server=/cluster.local/10.254.0.10#53
    domain-needed
    expand-hosts
    bogus-priv
    addn-hosts=/run/cluster/hosts
    

    The above use-case is for configuring a kubernetes cluster that ties in hostname resolution for dynamically "auto-discovered" hosts written to /run/cluster/hosts in addition to those statically defined in /etc/hosts.

    Thanks!

    opened by tonylambiris 0
Releases(v2.1.0)
Owner
Lex T
Lex T
Reverse Proxying + Static File Serving + Let's Encrypt + multiple hosts

Slashing This is a HTTPS server, which aims to replace my personal nginx usages. Currently, it serves Reverse Proxying (e.g. to a Python-Flask,Java,PH

Abby 3 Jul 29, 2021
Automatically update your Windows hosts file with the WSL2 VM IP address

Automatically update your Windows hosts file with the WSL2 VM IP address

null 1.4k Jan 9, 2023
Hprose 1.0 for Golang (Deprecated). Hprose 2.0 for Golang is here:

Hprose for Golang Introduction Installation Usage Http Server Http Client Synchronous Invoking Synchronous Exception Handling Asynchronous Invoking As

Hprose 139 Dec 15, 2022
Simple dashboard to check if hosts are up (via ICMP)

About ping-dashboard is a simple dashboard to quickly check if a large amount of hosts are up (via ICMP). Building $ cd /path/to/build/directory $ GOB

Kory Prince 0 Jan 14, 2022
Simple tool for connecting to remote hosts via ssh written on GO.

sshmenu is a simple tool for connecting to remote hosts via ssh written on GO. Great if you have trouble remembering IP addresses, hostnames, usernames or path to a key file.

Maxim Zanoga 3 Jul 21, 2022
KeeneticRouteToVpn is simple app updating Keenetic Router rules for some hosts to go through VPN interface.

KeeneticRouteToVpn KeeneticRouteToVpn is simple app updating Keenetic Router rules for some hosts to go through VPN interface. It has defaults values

Vasilii Blazhnov 8 Oct 8, 2022
[deprecated] A full-featured SPDY library for the Go language.

Deprecated With the release of Go1.6 and the addition of http2 to the standard library, this package is no longer under active development. It is high

Jamie Hall 120 Oct 1, 2022
Run commands on remote hosts, inspecting key indicators to manage infrastructure

inspector This is a very basic ssh helper tool to manage a smaller (few 100s up to a few 1000s) fleet of servers. The main point of inspector is to pr

null 17 Mar 3, 2022
Fetches one or more DNS zones via AXFR and dumps in Unix hosts format for local use

axfr2hosts About axfr2hosts is a tool meant to do a DNS zone transfer in a form of AXFR transaction of one or more zones towards a single DNS server a

Dinko Korunic 8 Aug 9, 2022
Prometheus exporter for ping metrics such as RTT, packet loss, and jitter to any number of hosts.

ping_exporter Command ping_exporter provides a Prometheus exporter for ping metrics such as RTT, packet loss, and jitter to any number of hosts. Usage

Beard Slayer 7 Sep 24, 2022
Project helps to identify the network, broadcast address and no of possible hosts

network_identifier Project helps to identify the network, broadcast address and no of possible hosts for Ipv4 address To use it directly as a go file

Vedant Pareek 0 Dec 3, 2021
Quickly find all IPv6 and IPv4 hosts in a LAN.

invaentory Quickly find all IPv6 and IPv4 hosts in a LAN. Overview ?? This project is a work-in-progress! Instructions will be added as soon as it is

Felix Pojtinger 7 May 17, 2022
DeepCopy a portable app that allows you to copy all forms of specified file types from your entire file system of the computer

DeepCopy a portable app that allows you to copy all forms of specified file types from your entire file system of the computer

subrahmanya  s hegade 1 Dec 20, 2021
Generates file.key file for IPFS Private Network.

ipfs-keygen Generates file.key file for IPFS Private Network. Installation go get -u github.com/reixmor/ipfs-keygen/ipfs-keygen Usage ipfs-keygen > ~/

Camilo Abel Monreal Aguero 0 Jan 18, 2022
Http Plugin Management For Golang

Http Plugin Management Install & Use Inline plugin PluginManager Example EnvoyPlugin Example 中文 Http Plugin Management Install & Use Use the following

null 4 Dec 7, 2022
gNXI Tools - gRPC Network Management/Operations Interface Tools

gNxI Tools gNMI - gRPC Network Management Interface gNOI - gRPC Network Operations Interface A collection of tools for Network Management that use the

Google 227 Dec 15, 2022
CDN for Open Source, Non-commercial CDN management

CDN Control Official Website: https://cluckcdn.buzz Documentation (Traditional Chinese): https://cluckcdn.buzz/docs/ 简体中文 README: README_CN.md Please

CluckCDN 2 Dec 20, 2021
COGS: COnfiguration manaGement S.

COGS: COnfiguration manaGement S cogs is a cli tool that allows generation of configuration files through different references sources. Sources of ref

Bestow 3 Dec 17, 2021