Netbox Automation with module pynetbox

477 views
Skip to first unread message

Erwan

unread,
Oct 7, 2019, 11:37:41 AM10/7/19
to NetBox
Hello guys,

I have a question about the interface-interconnections.
In a previous version of netbox, I seem to have succeeded in automating the interconnections between the interfaces of my devices. (sorry it was 6 months ago ^ _ ^ ')
Now, I notice that the creation of the interconnections is not available in "/api/docs/".
Is it now impossible to automate this task?

### Environment
* Python version:  3.7
* NetBox version:  2.6.5

Brian Candler

unread,
Oct 7, 2019, 1:56:44 PM10/7/19
to NetBox
For new versions of Netbox, you create "cables" to connect interfaces, rather than directly connecting interfaces.

There is also the new concept of of passive "front ports" and "rear ports", which are used to model patch panels. A front port is internally connected to a rear port on the same device.  Hence, you can now create a cable between an interface and a front port on PP1; a cable from the corresponding rear port on PP1 to a rear port on PP2; and a cable from the corresponding front port on PP2 to another interface.

When you do this, the two interfaces will appear to be connected to each other, but you can also trace the physical path in the GUI.

Erwan

unread,
Oct 9, 2019, 1:13:24 PM10/9/19
to NetBox
Hello,

Thanks you for your answer.

I developped a python scrypt to automate this but I'm having some problems.

Here is the data I send to my netbox:
"
{'termination_a_id': '11120',
'termination_a_type': 'dcim.interface',
'termination_b_id': '11121',
'termination_b_type': 'dcim.interface',
'terminaton_a': {'device': {'id': 398, 'name': 'v3aw33-lsw01'},
'id': '11120',
'name': 'ETH1/31'},
'terminaton_b': {'device': {'id': 399, 'name': 'v3aw33-lsw02'},
'id': '11121',
'name': 'ETH1/31'}}
"
All the information is correct but I note that the information sent to my netbox are different and false ...

"MGMT <> ETH1/1" and an reversal between termination_a and b


Is it a bug or did anyone manage to make it work?

Thx

Brian Candler

unread,
Oct 9, 2019, 3:49:26 PM10/9/19
to NetBox
I read that three times, and I still cannot make sense of it.   "the information sent to my netbox are different and false" - sounds like you intentionally sent wrong information to Netbox??

Please show:

(1) What you actually sent to Netbox

(2) What you saw in Netbox afterwards (e.g. from querying data back out through the API)

(3) What you think you should have seen instead

Erwan

unread,
Oct 10, 2019, 8:30:55 AM10/10/19
to NetBox

Hello

Sorry for the bad explanation.


(1) What you actually sent to Netbox

I would like to connect my devices v3aw33-lsw01 port ETH1/31 toward v3aw33-lsw02 port ETH1/31.

Here is a the data contained into my dictionnary.

 

{'termination_a_id': '11120',
'termination_a_type': 'dcim.interface',
'termination_b_id': '11121',
'termination_b_type': 'dcim.interface',
'terminaton_a': {'device': {'id': 398, 'name': 'v3aw33-lsw01'},
'id': '11120',
'name': 'ETH1/31'},
'terminaton_b': {'device': {'id': 399, 'name': 'v3aw33-lsw02'},
'id': '11121',
'name': 'ETH1/31'}}



(2) What you saw in Netbox afterwards (e.g. from querying data back out through the API)

 

I observe on my netbox a different result


The reuqest with the previous dictionnary:

   - new_interco = nb.dcim.cables.create(einterco)

And the result when I execute a "print" on the "new_interco"

- MGMT <> ETH1/1

screenshot.jpg

Jeremy Stretch

unread,
Oct 10, 2019, 9:07:05 AM10/10/19
to Erwan, NetBox
The termination_a and termination_b fields are read-only. This is the only portion of the data which NetBox looks at when creating a cable:

{

    'termination_a_id': '11120',
    'termination_a_type': 'dcim.interface',
    'termination_b_id': '11121',
    'termination_b_type': 'dcim.interface'
}

You'll need to ensure those are the correct IDs for the interfaces you want to connect.

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/cf65066c-1b59-4ddf-bed5-5931f0ff93f1%40googlegroups.com.


--
Jeremy Stretch
Sr. Network Automation Engineer
Network to Code, LLC

Brian Candler

unread,
Oct 10, 2019, 9:26:32 AM10/10/19
to NetBox
I note that the release notes for 2.6.0 say:

-----

API Support for Specifying Related Objects by Attributes (#3077)

Previously, specifying a related object in an API request required knowing the primary key (integer ID) of that object.
For example, when creating a new device, its rack would be specified as an integer:

{
    "name": "MyNewDevice",
    "rack": 123,
    ...
}

The NetBox API now also supports referencing related objects by a set of sufficiently unique attrbiutes. For example, a
rack can be identified by its name and parent site:

{
    "name": "MyNewDevice",
    "rack": {
        "site": {
            "name": "Equinix DC6"
        },
        "name": "R204"
    },
    ...
}
-----

Does this not work for terminations?  (Although certainly the OP should not be specifying termination_id *and* termination at the same time)

Jeremy Stretch

unread,
Oct 10, 2019, 9:40:11 AM10/10/19
to Brian Candler, NetBox
That's true for normal foreign keys, but termination_a and termination_b are generic foreign keys, which represent a relationship based on two other keys (the respective ID and type fields). The generic foreign key field itself is read-only in the API.

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.

Erwan

unread,
Oct 11, 2019, 5:49:08 AM10/11/19
to NetBox

Thank you for these explanations. I understand better now.

 

I'm now trying to get the interface ID but I can not do it.

 

I use this:     

 

             test = nb.dcim.interfaces.get (

                 device = Sedev,

                 name = srint,

             )

 

Here is the result that sends my request:

{'device': {'id': 398, 'name': 'v3aw33-lsw01'}, 'name': 'ETH1 / 31'}

 

But I get a

"none"

However, the device and the interface exist.

Capture d’écran 2019-10-11 à 11.39.46.png


 

 

Do you know how to retrieve it?

 

Brian Candler

unread,
Oct 13, 2019, 9:09:45 AM10/13/19
to NetBox
Good question.  Like most REST APIs, Netbox's has poorly specified semantics.

I had to experiment, but I found the following which work for me:


Translating to pynetbox they become:

>>> nb.dcim.interfaces.get(device="testdev",name="en0")

>>> nb.dcim.interfaces.get(device_id=381,name="en0")

So you were close, but it looks like you have to pass the device name or id, not the device object: i.e.

             test = nb.dcim.interfaces.get (

                 device_id = Sedev.id,

                 name = srint,

             )


(Note: although selecting by device name may sometimes be more convenient than id, it won't work for unnamed devices or when device names are no longer globally unique)

Erwan

unread,
Oct 16, 2019, 10:43:31 AM10/16/19
to NetBox
Hello,

Thx! It's working now. :-)
Reply all
Reply to author
Forward
0 new messages