Question: Create Interface connection via API (curl / bash)

1,430 views
Skip to first unread message

b2c

unread,
Jan 24, 2018, 5:45:57 AM1/24/18
to NetBox
hi@all,

I'm trying to figure out how to create interface connections between devices via API calls from the shell via curl. I browsed the swagger API docs and tried to recreate the command it proposed, but I always get the error

{
 
"detail": "Not found."
}

I try to create the interfaceconnection with this request:

curl -X PUT -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" --data '{
   "interface_a": "1250",
   "interface_b": "2676",
   "connection_status": "Connected"
 }'
\
'http://127.0.0.1:8001/api/dcim/interface-connections/212/?connection_status=Connected&device=40'
I also tried swapping the "device=" variable to the device name but no luck.


The interfaces with the IDs '1250' and '2767' exist, I can find them via an API call like this:

curl -X GET -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" 'http://127.0.0.1:8001/api/dcim/interface-connections/1250/'
{
 
"id": 1250,
 
"device": {
   
"id": 40,
   
"url": "http://127.0.0.1:8001/api/dcim/devices/40/",
   
"name": "serv64.ix",
   
"display_name": "server1"
 
},
 
"name": "device1",
 
"form_factor": {
   
"label": "1000BASE-T (1GE)",
   
"value": 1000
 
},
 
"enabled": true,
 
"lag": null,
 
"mtu": null,
 
"mac_address": "02:4D:05:C1:F2:C9",
 
"mgmt_only": true,
 
"description": "",
 
"is_connected": false,
 
"interface_connection": null,
 
"circuit_termination": null
}

What am I missing or doing wrong?

Thanks and regards,
david

Max

unread,
Jan 24, 2018, 6:09:22 AM1/24/18
to NetBox
I think you need to un-quote the values of the interface_a and interface_b fields, like this:


curl -X PUT -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" --data '{
   "interface_a": 1250,
   "interface_b": 2676,
   "connection_status": "Connected"
 }'
\
'http://127.0.0.1:8001/api/dcim/interface-connections/212/?connection_status=Connected&device=40'


Max

unread,
Jan 24, 2018, 6:16:03 AM1/24/18
to NetBox
Additionally, "connection_status" actually looks like this:

      "connection_status": {
        "label": "Connected",
        "value": true
      }

So something like


curl -X PUT -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" --data
'{
   "interface_a": 1250,
   "interface_b": 2676,
   "connection_status": true
 }'
\
'http://127.0.0.1:8001/api/dcim/interface-connections/212/?connection_status=Connected&device=40'

should work.


Am Mittwoch, 24. Januar 2018 11:45:57 UTC+1 schrieb b2c:

b2c

unread,
Jan 24, 2018, 8:58:15 AM1/24/18
to NetBox
hi,

thank you for the quick replay, but sorry to say that it still doesn't work:

curl -X PUT -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" --data '{ "interface_a": 1250, "interface_b": 2676, "connection_status": true }' 'http://127.0.0.1:8001/api/dcim/interface-connections/212/?device=40'

{
 
"detail": "Not found."
}

If I try to POST, I receive "Method not allowed". Also tried to swap "device=40" to the servername but I always get the same error "detail: not found"
Perhaps it has something todo with the fact, that I'm creating an new connetion with the ID "212" which I just made up? It's the next free ID, IDs 1-211 are already taken.

Max

unread,
Jan 24, 2018, 9:14:55 AM1/24/18
to NetBox
What's the "212" in the API address? I think that could be the cause of the error. Try POSTing to api/dcim/interface-connections/:

curl
-X POST -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" --data '{ "interface_a": 1250, "interface_b": 2676, "connection_status": true }' 'http://127.0.0.1:8001/api/dcim/interface-connections/'

David Gabriel

unread,
Jan 24, 2018, 10:04:04 AM1/24/18
to NetBox
hi,

the '212' would be the new connection ID which I 'invented' since 1-211 are already allocated for other connections. Anyway, if I try to POST


curl -X POST -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" --data '{ "interface_a": 1250, "interface_b": 2676, "connection_status": true }' 'http://127.0.0.1:8001/api/dcim/interface-connections/'

I get:
{
  "interface_a": [
    "This field is required."
  ],
  "interface_b": [
    "This field is required."
  ]
}

I tried with PUT too, but that method is "not allowed".

thanks for your patience and regards,
david

--
You received this message because you are subscribed to a topic in the Google Groups "NetBox" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netbox-discuss/cdCFML796LI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/4f4b74e6-8e82-4fd8-8747-4fe94e5b58af%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


Max

unread,
Jan 24, 2018, 10:32:43 AM1/24/18
to NetBox
Sorry, forgot the Content-Type header. This command works for me, and if it doesn't for you, I have no clue why.

curl -X POST -H "Authorization: Token <Token>" -H "Content-Type: application/json" -d '{"interface_a": 1, "interface_b": 3, "connection_status": true}' http://<HOST>:<PORT>/api/dcim/interface-connections/
To unsubscribe from this group and all its topics, send an email to netbox-discus...@googlegroups.com.

To post to this group, send email to netbox-...@googlegroups.com.

b2c

unread,
Jan 24, 2018, 10:38:40 AM1/24/18
to NetBox
Thank you so much, this worked for me too now!

curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <token>" -H "Accept: application/json; indent=2" --data '{ "interface_a": 1250, "interface_b": 2676, "connection_status": true }' 'http://127.0.0.1:8001/api/dcim/interface-connections/'

{
 
"id": 213,
Reply all
Reply to author
Forward
0 new messages