Skip to content

Instantly share code, notes, and snippets.

@nopcoder
Created May 28, 2018 19:27
Show Gist options
  • Save nopcoder/238c708b86d33fc59a0839677113a55b to your computer and use it in GitHub Desktop.
Save nopcoder/238c708b86d33fc59a0839677113a55b to your computer and use it in GitHub Desktop.
package main
import (
"encoding/csv"
"encoding/json"
"io/ioutil"
"net/http"
"os"
)
func main() {
// request amazon ip ranges data
res, _ := http.Get("https://ip-ranges.amazonaws.com/ip-ranges.json")
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
// parse json information
var aws struct {
SyncToken string `json:"syncToken"`
CreateDate string `json:"createDate"`
Prefixes []struct {
IPPrefix string `json:"ip_prefix"`
Region string `json:"region"`
Service string `json:"service"`
} `json:"prefixes"`
}
json.Unmarshal(body, &aws)
// ouput as csv
writer := csv.NewWriter(os.Stdout)
defer writer.Flush()
writer.Write([]string{"service", "region", "network"})
for _, p := range aws.Prefixes {
writer.Write([]string{p.Service, p.Region, p.IPPrefix})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment