-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
95 lines (85 loc) · 1.89 KB
/
main.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
package main
import (
"fmt"
"net"
"os"
"strconv"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "ASNTool",
Short: "Simple ASN Lookup Tool",
}
var asnCmd = &cobra.Command{
Use: "asn",
Long: "Get ASN informations from IP Addresses",
Example: "ASNTool asn [IP] [IP] ...",
Run: getAsn,
}
var netCmd = &cobra.Command{
Use: "net",
Long: "Get IP ranges from ASNs",
Example: "ASNTool net [ASN] [ASN] ... (without AS prefix)",
Run: getNetBlocks,
}
func main() {
rootCmd.AddCommand(asnCmd)
rootCmd.AddCommand(netCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func getAsn(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
for _, arg := range args {
address := net.ParseIP(arg)
if address == nil {
continue
}
asnRecord, err := IPToASRecord(address.String())
if err != nil {
printError(fmt.Sprintf("[-] IP Address: %s\n", address.String()))
continue
}
printSuccess(fmt.Sprintf("[+] IP Address: %s\n", address.String()))
fmt.Println("ASN:", asnRecord.ASN)
fmt.Println("Prefix:", asnRecord.Prefix)
fmt.Println("ASName:", asnRecord.ASName)
fmt.Println("CN:", asnRecord.CN)
fmt.Println("ISP:", asnRecord.ISP)
fmt.Println()
}
}
func getNetBlocks(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
for _, arg := range args {
asn, err := strconv.Atoi(arg)
if err != nil {
continue
}
asnNetBlocks, err := ASNToNetblocks(asn)
if err != nil {
printError(fmt.Sprintf("[-] AS: %d\n", asn))
continue
}
printSuccess(fmt.Sprintf("[+] AS: %d\n", asn))
for _, nb := range asnNetBlocks {
fmt.Println(nb)
}
fmt.Println()
}
}
func printError(msg string) {
color.New(color.FgHiRed).Fprintf(os.Stderr, msg)
}
func printSuccess(msg string) {
color.New(color.FgHiGreen).Fprintf(os.Stderr, msg)
}