Long time network engineer, did some perl a long time ago and am liking python pretty well but the pynetbox documentation is badly lacking IMO. If I were a python wizard I'm sure it would all be obvious but I'm not and it's really frustrating that more example weren't provided.
Many of the following examples were cadged from various places on the interwebs and HAVE NOT BEEN TESTED.
import pynetbox
NETBOX = 'https://netbox.fq.dn/'
nb = pynetbox.api(NETBOX, get_token('nb'))
get_token() is function that fetches the token from a hidden file in the home dir (~) that's named token_nb
. I've added it here as a separate file.
response = nb.dcim.devices.all()
response = nb.ipam.prefixes.all()
response = nb.ipam.ip_addresses.all()
response = nb.ipam.vlans.get(vlanid)
response = nb.dcim.devices.get(serial=tgt_sn)
response_list = nb.dcim.devices.filter(query)
response_list = nb.ipam.prefixes.filter(query)
response_list = nb.ipam.vlans.filter(query)
response_list = nb.tenancy.tenants.filter(query)
response_list = nb.dcim.interfaces.filter(device='DEV_NAME')
response = nb.ipam.ip_addresses.get(name=line)
response.delete
response = nb.dcim.devices.get(name=old_name)
response.name = new_name
response.save()
int_a = nb.dcim.interfaces.get(name='xe-4/0/16', device='BLAHSWITCH')
int_b = nb.dcim.interfaces.get(name='eth3', device='BLAHHOST')
nb.dcim.interface_connections.create(
interface_a=int_a.id,
interface_b=int_b.id
)
netbox.dcim.devices.create(
name='test',
device_role=1,
)
response = nb.dcim.interfaces.create(
device=721,
name="Eth1/3",
form_factor=1200,
enabled=True,
)
{ 'id': 13131,
'device': {'id': 721, 'url': 'https://netbox/api/dcim/devices/721/',
'name': 'TEST01', 'display_name': 'TEST01'
},
'name': 'Eth1/3',
'form_factor': {'value': 1200, 'label': 'SFP+ (10GE)'},
'enabled': True, 'lag': None, 'mtu': None, 'mac_address': None,
'mgmt_only': False, 'description': '', 'is_connected': False,
'interface_connection': None, 'circuit_termination': None,
'mode': None, 'untagged_vlan': None, 'tagged_vlans': [], 'tags': []
}
Figured out how to iterate through interfaces on a given device and update some fields on the interfaces. In case anyone else was curious, use the NetBox API docs to know what to use for object properties. (/api/docs/ on your NetBox)
Here is an example. (Sorry it's ugly. I included a screenshot. Not sure how to put formatted code in here. The "code" button in the comment toolbar isn't working as expected when I preview.)
`import pynetbox
nb = pynetbox.api(
'http://ansible.emu.edu:8000',
token='0123456789abcdef0123456789abcdef01234567'
)
if name == 'main':
# print(*nb.dcim.devices.all(), sep="\n")
# This is a recordset: https://pynetbox.readthedocs.io/en/latest/response.html#pynetbox.core.response.RecordSet
core_interfaces = nb.dcim.interfaces.filter('irb', device='core-0')
# print(*core_interfaces, sep="\n")
for interface in core_interfaces:
name = interface.name
interface_id = str(interface.id)
unit = name[4:]
if int(unit) < 319:
continue
int_obj = nb.dcim.interfaces.get(interface_id)
# print(unit + " (" + interface_id + ")")
# print(int_obj.name)
vlan = nb.ipam.vlans.get(vid=unit)
if vlan is None:
continue
result = int_obj.update({
"mode": "access",
"untagged_vlan": vlan.id
})
print(int_obj.name + " - " + vlan.name + " - id: " + str(vlan.id) + " - result: " + str(result))
`