Last active
August 14, 2020 20:25
-
-
Save mathershifter/3fa1ce9162ce870d864cfb683802f3be to your computer and use it in GitHub Desktop.
Golang eAPI example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"github.com/aristanetworks/goeapi" | |
) | |
type ShowIsisDatabase struct { | |
Vrfs map[string]struct { | |
IsisInstances map[string]struct { | |
Level map[string]struct { | |
Lsps map[string]struct { | |
IntermediateSystemType string `json:"intermediateSystemType"` | |
Sequence int64 `json:"sequence"` | |
Checksum int64 `json:"checksum"` | |
ExpiryTime float64 `json:"expiryTime"` | |
Hostname struct { | |
Name string `json:"name"` | |
} `json:"hostname"` | |
RouterCapabilities []struct { | |
RouterID string `json:"routerId"` | |
} `json:"routerCapabilities"` | |
} `json:"lsps"` | |
} `json:"level"` | |
} `json:"isisInstances"` | |
} `json:"vrfs"` | |
} | |
func (s *ShowIsisDatabase) GetCmd() string { | |
return "show isis database detail" | |
} | |
type ShowIPRouteBGP struct { | |
// ShowIPBGP Structure for 'show ip route bgp' results | |
Vrfs map[string]struct { | |
Routes map[string]struct { | |
Vias []struct { | |
TunnelDescriptor struct { | |
TunnelType string `json:"tunnelType"` | |
TunnelAddressFamily string `json:"tunnelAddressFamily"` | |
TunnelEndPoint string `json:"tunnelEndPoint"` | |
} `json:"tunnelDescriptor"` | |
Interface string `json:"interface"` | |
NexthopAddr string `json:"nexthopAddr"` | |
Vias []struct { | |
Interface string `json:"interface"` | |
LabelStack []int32 `json:"labelStack"` | |
NexthopAddr string `json:"nexthopAddr"` | |
} `json:"vias"` | |
} `json:"vias"` | |
} `json:"routes"` | |
} `json:"vrfs"` | |
} | |
func (s *ShowIPRouteBGP) GetCmd() string { | |
return "show ip route bgp" | |
} | |
type endpoint struct { | |
Hostname string | |
LspID string | |
Sequence int64 | |
} | |
type endpoints map[string]*endpoint | |
type target struct { | |
TargetAddr net.IP | |
ViaAddr net.IP | |
} | |
type targets []*target | |
func getTargets(node *goeapi.Node) targets { | |
// Now collect BGP routes | |
var tgts targets | |
bgpRoutes := &ShowIPRouteBGP{} | |
handle, _ := node.GetHandle("json") | |
handle.AddCommand(bgpRoutes) | |
if err := handle.Call(); err != nil { | |
panic(err) | |
} | |
for _, v := range bgpRoutes.Vrfs { | |
//fmt.Printf("VRF: %s\n") | |
for prefix, v := range v.Routes { | |
for _, v := range v.Vias { | |
ep := v.TunnelDescriptor.TunnelEndPoint | |
if ep == "" { | |
continue | |
} | |
targetAddr, targetNet, err := net.ParseCIDR(prefix) | |
if err != nil { | |
panic(err) | |
} | |
viaAddr, _, err := net.ParseCIDR(ep) | |
if err != nil { | |
panic(fmt.Sprintf("%s %s", err, ep)) | |
} | |
targetNetSize, _ := targetNet.Mask.Size() | |
if targetNetSize == 32 { | |
//fmt.Printf("%s/%d %s\n", targetAddr, targetNetSize, viaAddr) | |
tgts = append(tgts, &target{ | |
TargetAddr: targetAddr, | |
ViaAddr: viaAddr, | |
}) | |
} | |
} | |
} | |
} | |
return tgts | |
} | |
func getEndpoints(node *goeapi.Node) endpoints { | |
// Collect the ISIS database | |
eps := make(endpoints) | |
isisDb := &ShowIsisDatabase{} | |
handle, _ := node.GetHandle("json") | |
handle.AddCommand(isisDb) | |
if err := handle.Call(); err != nil { | |
panic(err) | |
} | |
for _, v := range isisDb.Vrfs { | |
for _, v := range v.IsisInstances { | |
for _, v := range v.Level { | |
for lspID, v := range v.Lsps { | |
if len(v.RouterCapabilities) > 0 { | |
routerID := v.RouterCapabilities[0].RouterID | |
eps[routerID] = &endpoint{ | |
Hostname: v.Hostname.Name, | |
LspID: lspID, | |
Sequence: v.Sequence, | |
} | |
} | |
} | |
} | |
} | |
} | |
return eps | |
} | |
func main() { | |
node, err := goeapi.Connect("http", "rbf153", "admin", "", 80) | |
if err != nil { | |
panic(err) | |
} | |
eps := getEndpoints(node) | |
fmt.Println("ISIS DB:") | |
for rid, ep := range eps { | |
fmt.Printf("%s %s %s %d\n", rid, ep.LspID, ep.Hostname, ep.Sequence) | |
} | |
fmt.Println() | |
tgts := getTargets(node) | |
fmt.Println("Valid targets:") | |
for _, tg := range tgts { | |
if _, ok := eps[tg.ViaAddr.String()]; ok { | |
fmt.Printf("Target: %s VIA: %s LSPID: %s HOST: %s\n", tg.TargetAddr.String(), tg.ViaAddr.String(), eps[tg.ViaAddr.String()].LspID, eps[tg.ViaAddr.String()].Hostname) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment