-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipg.go
106 lines (91 loc) · 3.3 KB
/
ipg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"net"
"os"
"github.com/alecthomas/kong"
"github.com/c-robinson/iplib"
"github.com/themimitoof/ipg/src"
"github.com/themimitoof/ipg/src/output"
)
var cliOutputSettings output.OutputInformationSettings
var cli struct {
Subnet string `arg:"" help:"IPv6 Subnet"`
Random bool `short:"r" help:"Generate a random IPv6 address on the given subnet."`
Name string `short:"n" default:"hostname" help:"Specify the hostname of a machine, an IPv6 address will be generated based on it."`
Silent bool `short:"s" help:"Only display values without their labels."`
Format string `name:"format" short:"f" enum:"console,json" default:"console" help:"Specify the type of output. Possible values: console, json"`
Address bool `short:"a" help:"Display the generated IP address."`
Reverse bool `short:"R" help:"Display the ARPA version of the IP address."`
DNSRecord bool `name:"dns" short:"d" help:"Returns a DNS record ready to paste to a DNS zone."`
ReverseRecord bool `name:"rrecord" short:"x" help:"Returns a rDNS record ready to paste to a DNS zone."`
DNSTTL int `name:"ttl" short:"t" default:"86400" help:"TTL value for DNS returned DNS records."`
}
func main() {
kong.Parse(
&cli,
kong.Description("A simple IPv6 generator for lazy netadmins."),
)
cliOutputSettings = output.OutputInformationSettings{
Address: cli.Address,
Reverse: cli.Reverse,
DNSRecord: cli.DNSRecord,
ReverseRecord: cli.ReverseRecord,
Format: cli.Format,
Silent: cli.Silent,
}
if !cli.Address && !cli.Reverse && !cli.DNSRecord && !cli.ReverseRecord {
cliOutputSettings.AllData = true
}
var ipNetwork iplib.Net6 = iplib.Net6FromStr(cli.Subnet)
// Check if the Subnet is valid or not.
if len(ipNetwork.IP()) == 0 {
os.Stderr.WriteString("The given subnet is not valid.\n")
os.Exit(1)
}
// Check if the subnet is not too small to be used
var cidr, _ = ipNetwork.Mask().Size()
if cidr > 126 {
os.Stderr.WriteString(
fmt.Sprintf("The given subnet (/%d) is too small to be used with ipg.\n", cidr),
)
os.Exit(1)
}
// Generate the new IP address using the given generation method
var generatedIp net.IP
if cli.Random {
generatedIp = src.GenerateRandomIP(ipNetwork)
} else {
generatedIp = src.GenerateIPFromHostname(ipNetwork, cli.Name)
}
var reverseIpAddr string = iplib.IPToARPA(generatedIp)
var ipAddr string = generatedIp.String()
var dnsRecord string = src.GenerateDNSRecord(ipAddr, cli.DNSTTL, cli.Name)
var reverseDnsRecord string = src.GenerateReverseDNSRecord(reverseIpAddr, cli.DNSTTL, cli.Name)
var cmdOutput []byte
// Render the output
if cli.Format == "json" {
cmdOutput = output.IpgJsonOutput{
Config: &cliOutputSettings,
Data: output.IpgOutputData{
Hostname: cli.Name,
IPAddress: generatedIp.String(),
IPReverse: reverseIpAddr,
DNSRecord: dnsRecord,
ReverseRecord: reverseDnsRecord,
},
}.Render()
} else {
cmdOutput = output.IpgConsoleOutput{
Config: &cliOutputSettings,
Data: output.IpgOutputData{
Hostname: cli.Name,
IPAddress: generatedIp.String(),
IPReverse: iplib.IPToARPA(generatedIp),
DNSRecord: dnsRecord,
ReverseRecord: reverseDnsRecord,
},
}.Render()
}
os.Stdout.Write(cmdOutput)
}