-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGcp_prefixes.go
More file actions
65 lines (55 loc) · 1.47 KB
/
Gcp_prefixes.go
File metadata and controls
65 lines (55 loc) · 1.47 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
// Struct to unmarshal JSON data
type CloudIPRanges struct {
Prefixes []struct {
IPv4Prefix string `json:"ipv4Prefix"`
IPv6Prefix string `json:"ipv6Prefix"`
} `json:"prefixes"`
}
func main() {
// URL of the JSON file
url := "https://www.gstatic.com/ipranges/cloud.json"
// Fetch data from the URL
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error fetching data from URL: %v\n", err)
return
}
defer resp.Body.Close()
// Decode JSON data
var cloudIPRanges CloudIPRanges
err = json.NewDecoder(resp.Body).Decode(&cloudIPRanges)
if err != nil {
fmt.Printf("Error decoding JSON: %v\n", err)
return
}
// Initialize slices for IPv4 and IPv6 prefixes
ipv4Prefixes := make([]string, 0)
ipv6Prefixes := make([]string, 0)
// Summarize the prefixes and add them to the respective slices
for _, prefix := range cloudIPRanges.Prefixes {
// Add IPv4 prefix to the IPv4 slice
if prefix.IPv4Prefix != "" {
ipv4Prefixes = append(ipv4Prefixes, prefix.IPv4Prefix)
}
// Add IPv6 prefix to the IPv6 slice
if prefix.IPv6Prefix != "" {
ipv6Prefixes = append(ipv6Prefixes, prefix.IPv6Prefix)
}
}
// Print summarized IPv4 prefixes
fmt.Println("Summarized IPv4 Prefixes:")
for _, ipv4Prefix := range ipv4Prefixes {
fmt.Println(ipv4Prefix)
}
// Print summarized IPv6 prefixes
fmt.Println("\nSummarized IPv6 Prefixes:")
for _, ipv6Prefix := range ipv6Prefixes {
fmt.Println(ipv6Prefix)
}
}