Reply

How to release a DHCP lease by IP address via WAPI (Python)

Member
Posts: 2
877     0

Hi,

 

I'm looking for python code to release a DHCP lease by IP address.
The GUI allows me too be only per IP address, using the WAPI it's easier to release multiple leases.
I have search the documentation but I couldn't find a way to do so.
I'm not that familiar with python either so I could use some help here.

 

First I need to know the exact URL (WAPI call) for this action with of course the IP address as a variable so I can use it for multiple IP addresses.

Any help is appreciated.

Thanks in advance.

Re: How to release a DHCP lease by IP address via WAPI (Python)

Adviser
Posts: 69
877     0

Hi,

 

I got this working with the below code. I am not very savvy with Python, I am sure this could be improved.

 

 

 

#!/usr/bin/python

import requests
from requests.auth import HTTPBasicAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

ip = raw_input("enter IP address for the lease: ")
user = ('admin')
password = ('infoblox')
url = ('https://10.192.32.240/wapi/v2.5/')
obj= ('lease?address='+ip)
func=('&_return_fields=')
state = ('&_return_fields=binding_state')
getresponse = requests.get(url+obj+func,auth=HTTPBasicAuth(user,password), verify = False)
getstate = requests.get(url+obj+state,auth=HTTPBasicAuth(user,password), verify = False)
print getstate.text
ref = getresponse.text[25:-9]
requests.delete(url+ref,auth=HTTPBasicAuth(user,password), verify = False)
print "The lease has been removed"
getstate = requests.get(url+obj+state,auth=HTTPBasicAuth(user,password), verify = False)
print getstate.text

Re: How to release a DHCP lease by IP address via WAPI (Python)

Techie
Posts: 5
877     0

This is already an old thread, but i am trying to do the same.

However, we have an HA pair as DHCP servers.

When i query a lease, i get two _ref returned.

When i delete both _ref, the lease is not removed , but the state switches to "BACKUP"

 

And i don't seem to know how i can also delete the lease on the backup server. Ideally, the lease should have state "FREE" again.

We are trying to remove a lot of devices from the network and we don't want to wait for the DHCP renewal timers to remove all leases before reconnecting the new devices. (and no, i don't want to change the timers, as we are only removing some specific devices from the network, we need to keep the others on default timers)

Re: How to release a DHCP lease by IP address via WAPI (Python)

Techie
Posts: 5
877     0

mmm it seems backup is also free in fact:

 

  • FREE: The address is available for lease, and will be handed out by the primary peer.
  • BACKUP: The address is available for lease, and will be handed out by the secondary peer.
Showing results for 
Search instead for 
Did you mean: 

Recommended for You