• Stars
    star
    307
  • Rank 135,700 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go library and CLI utility for /etc/hosts management.

txeh - /etc/hosts mangement

Etc Hosts Management Utility & Go Library

Go Report Card GoDoc

/etc/hosts Management

It is easy to open your /etc/hosts file in text editor and add or remove entries. However, if you make heavy use of /etc/hosts for software development or DevOps purposes, it can sometimes be difficult to automate and validate large numbers of host entries.

txeh was initially built as a golang library to support kubefwd, a Kubernetes port-forwarding utility utilizing /etc/hosts heavily, to associate custom hostnames with multiple local loopback IP addresses and remove these entries when it terminates.

A computer's /etc/hosts file is a powerful utility for developers and system administrators to create localized, custom DNS entries. This small go library and utility were developed to encapsulate the complexity of working with /etc/hosts directly by providing a simple interface for adding and removing entries in a /etc/hosts file.

txeh Utility

Install

MacOS homebrew users can brew install txn2/tap/txeh, otherwise see releases for packages and binaries for a number of distros and architectures including Windows, Linux and Arm based systems.

Complie and run from source (dependencies are vendored):

go run ./util/txeh.go

Use

The txeh CLI application allows command line or scripted access to /etc/hosts file modification.

Example CLI Usage:

 _            _
| |___  _____| |__
| __\ \/ / _ \ '_ \
| |_ >  <  __/ | | |
 \__/_/\_\___|_| |_| v1.1.0

Add, remove and re-associate hostname entries in your /etc/hosts file.
Read more including useage as a Go library at https://github.com/txn2/txeh

Usage:
  txeh [flags]
  txeh [command]

Available Commands:
  add         Add hostnames to /etc/hosts
  help        Help about any command
  remove      Remove a hostname or ip address
  version     Print the version number of txeh

Flags:
  -d, --dryrun         dry run, output to stdout (ignores quiet)
  -h, --help           help for txeh
  -q, --quiet          no output
  -r, --read string    (override) Path to read /etc/hosts file.
  -w, --write string   (override) Path to write /etc/hosts file.
# point the hostnames "test" and "test.two" to the local loopback
sudo txeh add 127.0.0.1 test test.two

# remove the hostname "test"
sudo txeh remove host test

# remove multiple hostnames 
sudo txeh remove host test test2 test.two

# remove an IP address and all the hosts that point to it
sudo txeh remove ip 93.184.216.34

# remove multiple IP addresses
sudo txeh remove ip 93.184.216.34 127.1.27.1

# remove CIDR ranges
sudo txeh remove cidr 93.184.216.0/24 127.1.27.0/28

# quiet mode will suppress output
sudo txeh remove ip 93.184.216.34 -q

# dry run will print a rendered /etc/hosts with your changes without
# saving it.
sudo txeh remove ip 93.184.216.34 -d

# use quiet mode and dry-run to direct the rendered /etc/hosts file
# to another file
sudo txeh add 127.1.27.100 dev.example.com -q -d > hosts.test

# specify an alternate /etc/hosts file to read. writing will
# default to the specified read path.
txeh add 127.1.27.100 dev2.example.com -q -r ./hosts.test

# specify a seperate read and write oath
txeh add 127.1.27.100 dev3.example.com -r ./hosts.test -w ./hosts.test2

txeh Go Library

Dependency:

go get github.com/txn2/txeh

Example Golang Implementation:

package main

import (
    "fmt"
    "strings"

    "github.com/txn2/txeh"
)

func main() {
    hosts, err := txeh.NewHostsDefault()
    if err != nil {
        panic(err)
    }

    hosts.AddHost("127.100.100.100", "test")
    hosts.AddHost("127.100.100.101", "logstash")
    hosts.AddHosts("127.100.100.102", []string{"a", "b", "c"})
    
    hosts.RemoveHosts([]string{"example", "example.machine", "example.machine.example.com"})
    hosts.RemoveHosts(strings.Fields("example2 example.machine2 example.machine.example.com2"))

    
    hosts.RemoveAddress("127.1.27.1")
    
    removeList := []string{
        "127.1.27.15",
        "127.1.27.14",
        "127.1.27.13",
    }
    
    hosts.RemoveAddresses(removeList)
    
    hfData := hosts.RenderHostsFile()

    // if you like to see what the outcome will
    // look like
    fmt.Println(hfData)
    
    hosts.Save()
    // or hosts.SaveAs("./test.hosts")
}

Build Release

Build test release:

goreleaser --skip-publish --rm-dist --skip-validate

Build and release:

GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --rm-dist

License

Apache License 2.0

More Repositories

1

kubefwd

Bulk port forwarding Kubernetes services for local development.
Go
3,676
star
2

txpdf

HTML to PDF microservice
XSLT
91
star
3

n2proxy

Contraband filtering reverse proxy for plain http and SSL.
Go
32
star
4

asws

Another static web server, plus JSON REST API for exposing specified directory listings. Useful for download pages or listings asset for web applications.
Go
22
star
5

irsync

rsync on interval, via command line binary or docker container. Server and IOT builds for pull or push based device content management.
Go
21
star
6

rxtx

Queue based data collector, transmitter with store-and-forward. Useful for online/offline data collection, back pressure buffering or general queuing. REST / HTTP post.
Go
18
star
7

p3y

A single binary reverse proxy written in go. It was developed for use in Kubernetes, to wrap services like Prometheus with simple BasicAuth and TLS encryption.
Go
17
star
8

datalab

Custom JupyterLab container for local-workstations and in-cluster Kubernetes Data Science, Machine Learning and IoT.
Dockerfile
12
star
9

provision

WIP: User and Account micro-platform.
Go
8
star
10

n2pdf

Alpine based container providing a stable wrapper for wkhtmltopdf
8
star
11

rtbeat

Rtbeat processes HTTP POST data from rxtx and publishes events into elasticsearch, logstash, kafka, redis or directly to log files.
Go
8
star
12

txtoken

txToken creates an HS256 JWT token from JSON retrieved from a remote endpoint resulting from a proxied JSON request body.
Go
8
star
13

amp

Kubernetes Dynamic Admission Control mutating Webhook proxy for Pods.
Go
7
star
14

mxt

Endpoint proxy for data transformation.
Go
6
star
15

docker-kubectl

Kubectl installed in an alpine based image from docker. Used for container building and k8s deployments in CI/CD.
Dockerfile
6
star
16

dmk

Declarative data migration.
Go
5
star
17

homebrew-tap

Homebrew Tap
Ruby
5
star
18

es

WIP: TXN2 Elasticsearch utilities
Go
5
star
19

xn2

pull, synchronize and package data on interval.
Go
5
star
20

jwtpxy

jwtpxy simplifies API security by pre-validating and parsing JWT web tokens into HTTP headers easily read by backend services.
Go
4
star
21

n2bot

Go
3
star
22

pvci

WIP: PVC Injector
Go
3
star
23

query

TNX2 Elasticsearch query system
Go
3
star
24

txagent

Docker based iot container orchestration for arm based devices (Raspberry Pi / Orange Pi / Tinkerboard) or traditional servers.
Go
3
star
25

service

Go
2
star
26

gpu-lab

GPU enabled JupyterLab containers.
Dockerfile
2
star
27

tm

WIP: TXN2 types model API
Go
2
star
28

amp-wh-example

Admission Mutation Proxy (txn2/amp) implementation example project.
Go
2
star
29

ok

OK message served by http, written in Go as a Kubernetes/Minikube tutorial.
Go
2
star
30

ack

TXN2 common API acknowledgement Go lib.
Go
2
star
31

micro

WIP: TXN2 micro server (gin-gonic wrapper)
Go
2
star
32

kwatch

Kafka utility container for Kubernetes
Go
2
star
33

kagent

Makefile
1
star
34

hugo-builder

Small container with Hugo, for building static sites.
Dockerfile
1
star
35

n2cas

Go
1
star
36

userpatches

Armbian userpatches for txn2
Shell
1
star
37

kdash

JavaScript
1
star
38

grape

WIP: TXN2 GRAfana Proxy for Elasticsearch data source.
Go
1
star
39

qlrx

WIP: TXN2 QL tcp receiver
Go
1
star
40

txwt

Makefile
1
star
41

curl

Apline curl container
Makefile
1
star
42

phc

Performance Hobby Cluster
1
star
43

www

Site
CSS
1
star
44

easy-novnc-container

Container for easy-novnc
Dockerfile
1
star
45

token

WIP: TXN2 JWT user token
Go
1
star
46

fxtx

WIP: TXN2 Fake transmission
Go
1
star
47

go-wkhtmltopdf

Golang commandline wrapper for wkhtmltopdf
Go
1
star
48

n2tok

JWT Token abstraction for TXN2 based services.
Go
1
star