Skip to content

Instantly share code, notes, and snippets.

@cardi
Created August 26, 2016 04:06
Show Gist options
  • Save cardi/0d88a3e76f6b65601e5552ba18b6089f to your computer and use it in GitHub Desktop.
Save cardi/0d88a3e76f6b65601e5552ba18b6089f to your computer and use it in GitHub Desktop.
iterating through all records of an mrt (routing) file with go-mrt
package main
import (
"log"
"os"
mrt "github.com/kaorimatz/go-mrt"
)
// basic bgp/mrt library: https://github.com/kaorimatz/go-mrt/
// full bgp library: https://github.com/osrg/gobgp
//
// go-mrt is useful because I only need to parse mrt files
func main() {
f, err := os.Open("updates-201608252015")
if err != nil {
log.Fatal(err)
}
defer f.Close()
mf := mrt.NewReader(f)
i := 0
// protect ourselves from panic (due to corrupt MRT files)
defer func() {
log.Printf("done processing")
if x := recover(); x != nil {
log.Printf("run time panic: %v, processing ended early", x)
}
}()
// iterate through all records
for {
record, err := mf.Next() // one day, I'll implement a Read()
if record == nil {
break
}
if err != nil {
log.Printf("error: %s, continuing...\n", err)
continue
}
// only care about timestamp
ts, _ := (*record).Timestamp().MarshalText()
log.Printf("%d %s\n", i, ts)
i = i + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment