Skip to content

Instantly share code, notes, and snippets.


scottdware/srx-ipam.go Secret

Last active Mar 14, 2016
What would you like to do?
package main
import (
// dynamicHosts parses the overall XML returned from the Infoblox query.
type dynamicHosts struct {
XMLName xml.Name `xml:"list"`
Hosts []hostEntry `xml:"value>ipv4addrs>list>value"`
// hostEntry parses the XML for each individual host.
type hostEntry struct {
Name string `xml:"host"`
Address string `xml:"ipv4addr"`
var (
searchString = "ware"
ipamGM = ""
ipamUser = "ibadmin"
ipamPass = "infoblox"
srxHost = ""
srxUser = "juniper"
srxPass = "Juniper123!"
groupName = "Dynamic-IPAM-Hosts"
// queryIPAM searches throughout Infoblox for the given search string...which matches the "comment" field
// within Infoblox and returns addresses that we will build our address-set for.
func queryIPAM() (*dynamicHosts, error) {
var data dynamicHosts
reqURL := fmt.Sprintf("https://%s/wapi/v1.0/record:host?comment~:=%s", ipamGM, searchString)
r := rested.NewRequest()
r.BasicAuth(ipamUser, ipamPass)
headers := map[string]string{
"Accept": "application/xml",
// Send our HTTP request to Infoblox.
resp := r.Send("get", reqURL, nil, headers, nil)
if resp.Error != nil {
err := xml.Unmarshal(resp.Body, &data)
if err != nil {
return nil, err
return &data, nil
func main() {
var srxConfig []string
// Run our query against IPAM/Infoblox to get our addresses.
d, err := queryIPAM()
if err != nil {
// Create our address entries first, and append them to our config.
for _, ae := range d.Hosts {
srxConfig = append(srxConfig, fmt.Sprintf("set security address-book global address %s %s/32\n", ae.Name, ae.Address))
// Create the address-set/group and assign the addresses we just created to it.
for _, as := range d.Hosts {
srxConfig = append(srxConfig, fmt.Sprintf("set security address-book global address-set %s address %s\n", groupName, as.Name))
// Connect to our SRX.
jnpr, err := junos.NewSession(srxHost, srxUser, srxPass)
if err != nil {
// Load our configuration into the SRX from the "srxConfig" variable we set earlier.
err = jnpr.Config(srxConfig, "set", false)
if err != nil {
// Commit the configuration to our SRX.
// Print the changes out to the console.
changes, _ := jnpr.ConfigDiff(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.