pynetbox mark ip as main

1,412 views
Skip to first unread message

Amr Rashad

unread,
Jun 9, 2018, 6:48:44 AM6/9/18
to NetBox
I'm able to create an IP address and assign it to an interface but can't manage to mark the ip as the main ip just by using pynetbox, Any help would be appreciated. 

Matthew Yauch

unread,
Jun 11, 2018, 11:48:52 AM6/11/18
to NetBox
When asking for help like this you should usually include the code that you've tried and any errors, output, or results received.  This would allow us to point out exactly where your issue lies.  Note that setting ONLY device.primary_ip appears to do nothing unless you also set the corresponding ip4/ip6 for it, too.  I didn't test very thoroughly, but the following code worked for me.  The no_ssl_verification is just a wrapper I copy pasted from the Interwebz to not get HTTPS errors.

import pynetbox
from nosslverify import no_ssl_verification

NETBOX_URL
= 'https://netbox.mydomain.com'
NETBOX_TOKEN
= '01189998819991197253'

with no_ssl_verification():
  netbox
= pynetbox.api(NETBOX_URL, token=NETBOX_TOKEN)
  device
= netbox.dcim.devices.get(name='test')
  ip
= netbox.ipam.ip_addresses.get(117)
  device
.primary_ip4 = ip
  device
.primary_ip = ip
  device
.save()




Amr Rashad

unread,
Jun 12, 2018, 6:04:55 AM6/12/18
to NetBox
Thank you for your help and here is my code:

```
#!/usr/bin/python2
import requests
import pynetbox
import socket
import subprocess
auth_token = '##'
hed = {'Authorization': 'Token ' + auth_token}
nb = pynetbox.api("http://website.com", token=auth_token)
# grab hostname
host = socket.gethostname()
a,b,c = host.split(".")
# create a new device aka server
nb.dcim.devices.create({"name": a, "device_role": 8, "device_type": 1, "site": 1, "rack": 1, "platform": 5})
response = requests.get(url, headers=hed)
data = response.json()
if_id = (data['results'][0]['id'])
#create an interface for the server
nb.dcim.interfaces.create({"name": "eth0", "device": if_id, "form_factor": 800, "mtu": 1400})
response = requests.get(url2, headers=hed)
data2 = response.json()
ip_id = (data2['results'][0]['id'])
print(ip_id)
cmd = "ip -4 -o addr | grep -E '38' | cut -d ' ' -f7 | sed 's/...$//'"
ip,error = subprocess.Popen(cmd, shell=True, executable="/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
cmd2 = "ip -4 -o addr | grep -E '38' | cut -d ' ' -f7 | cut -d'/' -f2"
net_mask,error = subprocess.Popen(cmd2, shell=True, executable="/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
ip_format = ip.rstrip() + "/" + net_mask
p = '"{}"'.format(ip_format)
print(p)
nb.ipam.ip_addresses.create(address=ip_format,status=1,interface=ip_id)
# adding ip as main ipv4
device = nb.dcim.devices.get(name=a)
ip = nb.ipam.ip_addresses.get(ip_id)
device.primary_ip4 = ip
device.primary_ip = ip
device.save()
```
when i added your code and ran it i got :
AttributeError: object has no attribute "primay_ip4"
But after adding the ip address as main from the web interface and then call the device.primary_ip4 it displays the ip address, any idea why ?

Amr Rashad

unread,
Jun 12, 2018, 7:11:03 AM6/12/18
to NetBox
It seems i was making the mistake of using an incorrect id value for the ip address, the code you provided works thank you.

Matthew Yauch

unread,
Jun 12, 2018, 8:56:16 AM6/12/18
to NetBox
You're welcome!  Glad you got it working.
Reply all
Reply to author
Forward
0 new messages