creating interface connections via api/pynetbox

1,545 views
Skip to first unread message

Erdem Kasnak

unread,
Jan 1, 2020, 3:21:09 PM1/1/20
to NetBox
Dear Experts,

Hope new year brings everyone health and success. 

I am new to Netbox. 

I want to create interface connections by help of pynetbox.

In the UI, interface connection is created via interface. In the api doc, as far as i understand, interface connection is not in the model of interface. And in the api docs, only get method is defined for interface connection.

I tried a couple of things, but couldn't figure out how t  do it. May someone help ?

For example i tried this, apparently as post method is not supported, it didnot get through :
int_a = nb.dcim.interfaces.get(name='GigabitEthernet0/3', device=16)
int_b = nb.dcim.interfaces.get(name='GigabitEthernet0/1', device=17)
nb.dcim.interface_connections.create( interface_a=int_a, interface_b=int_b)

Thanks and regards.

erdem

Brian Candler

unread,
Jan 1, 2020, 3:44:00 PM1/1/20
to NetBox
Assuming you're talking about a recent version of netbox (2.6+): "interface_connections" has gone, and what you now need is nb.dcim.cables.

Note that a cable doesn't have to link directly to another interface - it can go to a frontport on a patch panel, and the corresponding rearport goes across to another patch panel and so on.

There is also a readable "connected_endpoint" on the interface.  This is just a cache, saying what remote interface is at the other end of the chain of cables / frontports / rearports.

HTH,

Brian.

ranjeet

unread,
Jan 2, 2020, 12:30:29 AM1/2/20
to Brian Candler, NetBox
Hi Erdem/Brian ,

In my recent projects i did bulk import via pynetbox , below is an example of cable connection between 2 interfaces , This might help you with creating new interface connections .

=======================================================================================================================================
def cables_create(self):
for nodea,porta,nodeb,portb,hairpin,row in zip(self.list_sheet4_node_a,self.list_sheet4_port_a,
self.list_sheet4_node_b,self.list_sheet4_port_b,
self.list_sheet4_hairpin,range(len(self.list_sheet4_node_a))):
a_id=0
b_id=0
#print("{}:{}:{}:{}".format(nodea,porta,nodeb,portb))
if nodea!="" and nodeb!="" and porta!="" and portb!="":
try:
a_id=(self.nb.dcim.interfaces.get(name=porta, device=self.nb.dcim.devices.get(name=nodea))).id
b_id=(self.nb.dcim.interfaces.get(name=portb, device=self.nb.dcim.devices.get(name=nodeb))).id
except AttributeError:
print("ROW : {} Device {} Interface {} or Device {} Interface {} not found".format(row,nodea,porta,nodeb,portb))
if a_id !=0 and b_id !=0:
data_input = {
"termination_a_type": "dcim.interface",
"termination_a_id": a_id,
"termination_b_type": "dcim.interface",
"termination_b_id": b_id,
"status": 'true'
}

try:
result = self.nb.dcim.cables.create(data_input)
print("ROW : {} cable Connection Made successfully {} : {} <-> {} : {}".format(row,nodea,porta,nodeb,portb))
except (self.request_error,AttributeError):
print("ROW : {} Cable Already Connected {}:{}:{}:{}".format(row,nodea,porta,nodeb,portb) )
else:
print("ROW : {} Invalid - Details not Enough for Cable Connection {}:{}:{}:{}".format(row,nodea,porta,nodeb,portb))

=======================================================================================================================================


Regards :-
Ranjeet
        
                  
  


--
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/0c2ba46b-0c4f-4583-b159-24f4a6b30fd0%40googlegroups.com.

Erdem Kasnak

unread,
Jan 2, 2020, 4:22:22 PM1/2/20
to NetBox
Brian / Ranjeet,
Many thanks for quick responses. 
Brian, you are right. 2.6.8
Regards
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages