diff options
author | Franck Cuny <franck@fcuny.net> | 2022-10-04 17:53:28 -0700 |
---|---|---|
committer | Franck Cuny <franck@fcuny.net> | 2022-10-04 17:53:28 -0700 |
commit | 5da35c9ae3396002e3c8d4de8edbdf85ecf2569d (patch) | |
tree | f108d6fe7e0133130b1a1a639b36ecf703b9121d /tools/ipconverter/main.go | |
parent | ops(terraform): individual actions for init/plan/apply (diff) | |
download | world-5da35c9ae3396002e3c8d4de8edbdf85ecf2569d.tar.gz |
ref(tools/ipconverter): rewrite the tool in python
No need to do this with Go, a python script is fine. We also don't need to set shell aliases for this: when we install the tool, we can create symbolic links to `ip2int` and `int2ip`.
Diffstat (limited to 'tools/ipconverter/main.go')
-rw-r--r-- | tools/ipconverter/main.go | 63 |
1 files changed, 0 insertions, 63 deletions
diff --git a/tools/ipconverter/main.go b/tools/ipconverter/main.go deleted file mode 100644 index 42baf7c..0000000 --- a/tools/ipconverter/main.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "encoding/binary" - "fmt" - "math/big" - "net" - "os" - "path" - "strconv" -) - -func main() { - if len(os.Args) < 2 { - fmt.Fprintf(os.Stderr, "usage: %s <ip2int|int2ip> <values>\n", os.Args[0]) - os.Exit(1) - } - - cmd := path.Base(os.Args[1]) - - switch cmd { - case "ip2int": - for _, ip := range os.Args[2:] { - r, err := IPtoInt(ip) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to parse %s to an int: %s\n", ip, err) - continue - } - fmt.Printf("%s\t%d\n", ip, r) - } - case "int2ip": - for _, ip := range os.Args[2:] { - r, err := IPtoN(ip) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to parse %s to an addresse IP: %s\n", ip, err) - continue - } - fmt.Printf("%s\t%s\n", ip, r) - } - default: - fmt.Fprintf(os.Stderr, "`%s' is not a supported command\n", cmd) - os.Exit(1) - } -} - -func IPtoInt(ip string) (*big.Int, error) { - i := net.ParseIP(ip) - if len(i.To4()) == 4 { - return big.NewInt(0).SetBytes(i.To4()), nil - } else { - return nil, fmt.Errorf("%s is not an IPv4 address", ip) - } -} - -func IPtoN(ip string) (string, error) { - r, err := strconv.Atoi(ip) - if err != nil { - return "", fmt.Errorf("failed to parse %s to an int: %s", ip, err) - } - newIP := make(net.IP, 4) - binary.BigEndian.PutUint32(newIP, uint32(r)) - return newIP.To4().String(), nil -} |