tools.go_host

Yohn Y. 2020-12-22

0:520c8621f5ee Go to Latest

tools.go_host/host.go

.. init

History
1 package main
3 import (
4 "fmt"
5 "net"
6 "os"
8 idna "golang.org/x/net/idna"
9 )
11 func say_err(msg interface{}) {
12 _, err := fmt.Fprintf(os.Stderr, "ERROR: %s\n", msg)
13 if err != nil {
14 panic(err)
15 }
17 os.Exit(1)
18 }
20 func main() {
21 if len(os.Args) < 2 {
22 say_err("Need hostname or IP as argument")
23 }
25 arg := os.Args[1]
27 ip := net.ParseIP(arg)
29 if ip != nil {
30 res, err := net.LookupAddr(ip.String())
31 if err != nil {
32 say_err(err)
33 }
35 for _, i := range res {
36 fmt.Println(i)
37 }
39 } else {
40 hostname, err := idna.ToASCII(arg)
41 if err != nil {
42 say_err(err)
43 }
45 res, err := net.LookupHost(hostname)
46 if err != nil {
47 say_err(err)
48 }
50 for _, i := range res {
51 fmt.Println(i)
52 }
54 }
56 }