0

Get Device

by
Published today

Get one device by id, as raw NetBox JSON (platform, custom_fields, primary IPs…).

Script netbox Verified

The script

Submitted by hugo989 Python3
Verified 3 hours ago
1
# py312
2
# requirements:
3
# pynetbox==7.9.0
4

5

6
from typing import TypedDict
7

8
import pynetbox
9

10

11
class netbox(TypedDict):
12
    url: str
13
    token: str
14

15

16
def api(auth: netbox):
17
    # pynetbox adds /api itself; the resource URL may already end with it.
18
    return pynetbox.api(
19
        auth["url"].rstrip("/").removesuffix("/api"), token=auth["token"]
20
    )
21

22

23
def main(
24
    auth: netbox,
25
    device_id: int,
26
):
27
    """
28
    Get Device
29
    Get one device by id, as raw NetBox JSON (platform, custom_fields, primary IPs…).
30
    """
31
    record = api(auth).dcim.devices.get(device_id)
32
    if record is None:
33
        raise ValueError(f"No device with id {device_id}")
34
    return dict(record)
35