tools.go_host

Yohn Y. 2020-12-23 Parent:host.go@520c8621f5ee

1:75309599b04f Go to Latest

tools.go_host/src/host.go

+ Сортировка вывода

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/host.go	Wed Dec 23 11:42:02 2020 +0300
     1.3 @@ -0,0 +1,60 @@
     1.4 +package main
     1.5 +
     1.6 +import (
     1.7 +	"fmt"
     1.8 +	"net"
     1.9 +	"os"
    1.10 +	"sort"
    1.11 +
    1.12 +	idna "golang.org/x/net/idna"
    1.13 +)
    1.14 +
    1.15 +func say_err(msg interface{}) {
    1.16 +	_, err := fmt.Fprintf(os.Stderr, "ERROR: %s\n", msg)
    1.17 +	if err != nil {
    1.18 +		panic(err)
    1.19 +	}
    1.20 +
    1.21 +	os.Exit(1)
    1.22 +}
    1.23 +
    1.24 +func out_res(items []string) {
    1.25 +	sort.Strings(items)
    1.26 +	for _, i := range items {
    1.27 +		fmt.Println(i)
    1.28 +	}
    1.29 +}
    1.30 +
    1.31 +func main() {
    1.32 +	if len(os.Args) < 2 {
    1.33 +		say_err("Need hostname or IP as argument")
    1.34 +	}
    1.35 +
    1.36 +	arg := os.Args[1]
    1.37 +
    1.38 +	ip := net.ParseIP(arg)
    1.39 +
    1.40 +	if ip != nil {
    1.41 +		res, err := net.LookupAddr(ip.String())
    1.42 +		if err != nil {
    1.43 +			say_err(err)
    1.44 +		}
    1.45 +
    1.46 +		out_res(res)
    1.47 +
    1.48 +	} else {
    1.49 +		hostname, err := idna.ToASCII(arg)
    1.50 +		if err != nil {
    1.51 +			say_err(err)
    1.52 +		}
    1.53 +
    1.54 +		res, err := net.LookupHost(hostname)
    1.55 +		if err != nil {
    1.56 +			say_err(err)
    1.57 +		}
    1.58 +
    1.59 +		out_res(res)
    1.60 +
    1.61 +	}
    1.62 +
    1.63 +}