netbox demo
阅读原文时间:2023年07月09日阅读:1

参考链接:https://pypi.org/project/python-netbox/

# python 参考链接

https://ttl255.com/pynetbox-netbox-python-api-client-p1-getting-info/

https://ttl255.com/pynetbox-netbox-python-api-client-part-2-creating-objects/

https://ttl255.com/pynetbox-netbox-python-api-client-part-3-updates-and-deletions/

https://ttl255.com/pynetbox-netbox-python-api-client-part-4-tags-auto-assing-prefixes/

https://pynetbox.readthedocs.io/en/latest/

# 下载python包

pip3 install python-netbox

# golang 示例:

package main

import (
"context"
"github.com/netbox-community/go-netbox/netbox"
"github.com/netbox-community/go-netbox/netbox/client/dcim"
"github.com/netbox-community/go-netbox/netbox/client/ipam"
"github.com/netbox-community/go-netbox/netbox/models"

    log "github.com/sirupsen/logrus"  

)

func main() {
//c := netbox.NewNetboxAt("x.x.x.x:9999")
c := netbox.NewNetboxWithAPIKey("x.x.x.x:9999", "1f3c19b9586d875e74a36fe227d3e54d87ecf2be")

    // List site  
    req := dcim.NewDcimSitesListParams()  
    siteList, err := c.Dcim.DcimSitesList(req, nil)  
    if err != nil {  
            log.Fatalf("Cannot get sites list: %v", err)  
            return  
    }  
    log.Infof("siteList: %v", siteList)

    for \_, v := range siteList.Payload.Results {  
            log.Infof("site id  : %v", v.ID)  
            log.Infof("site name: %v", v.Name)  
    }

    // create vrf  
    var vrfID int64  
    {  
            tags := \[\]string{""}  
            name := "test\_vrf"  
            data := &models.WritableVRF{  
                    Tags:          tags,  
                    EnforceUnique: true,  
                    Name:          &name,  
            }  
            p := ipam.NewIpamVrfsCreateParamsWithContext(context.TODO())  
            p = p.WithData(data)  
            vrf, err := c.Ipam.IpamVrfsCreate(p, nil)  
            if err != nil {  
                    log.Fatalf("Cannot create vrf: %v", err)  
                    return  
            }  
            log.Infof("vrf: %v", vrf)  
            vrfID = vrf.Payload.ID  
    }

    // create prefix  
    var prefixID int64  
    {  
            prefix := "100.100.100.0/24"  
            tags := \[\]string{"abcdef"}  
            data := &models.WritablePrefix{  
                    Prefix:      &prefix,  
                    Vrf:         &vrfID,  
                    Description: "",  
                    IsPool:      true,  
                    Tags:        tags,  
            }  
            p := ipam.NewIpamPrefixesCreateParams()  
            p = p.WithContext(context.TODO())  
            p = p.WithData(data)  
            pf, err := c.Ipam.IpamPrefixesCreate(p, nil)  
            if err != nil {  
                    log.Fatalf("Cannot create prefix: %v", err)  
                    return  
            }  
            log.Infof("prefix: %v", pf)  
            prefixID = pf.Payload.ID  
    }

    // allocate ip addr  
    {  
            tags := \[\]string{""}  
            data := &models.WritablePrefix{  
                    Vrf:         &vrfID,  
                    Tags:        tags,  
                    Description: "",  
            }  
            p := ipam.NewIpamPrefixesAvailableIpsCreateParamsWithContext(context.TODO())  
            p = p.WithData(data)  
            p = p.WithID(prefixID)  
            addr, err := c.Ipam.IpamPrefixesAvailableIpsCreate(p, nil)  
            if err != nil {  
                    log.Fatalf("Cannot allocate ip addr: %v", err)  
                    return  
            }  
            log.Infof("allocate addr: %v", addr)  
    }

    // create ip addr  
    {  
            ipAddr := "100.100.100.100/24"  
            tags := \[\]string{""}  
            data := &models.WritableIPAddress{  
                    Address:     &ipAddr,  
                    Vrf:         &vrfID,  
                    Tags:        tags,  
                    Description: "",  
            }  
            p := ipam.NewIpamIPAddressesCreateParamsWithContext(context.TODO())  
            p = p.WithData(data)  
            addr, err := c.Ipam.IpamIPAddressesCreate(p, nil)  
            if err != nil {  
                    log.Fatalf("Cannot allocate ip addr: %v", err)  
                    return  
            }  
            log.Infof("create addr: %v", addr)  
    }  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章