Reply

Infoblox wapi python help with adding a new host to dns

[ Edited ]
New Member
Posts: 5
8405     0

I am trying to create a host if it does not exist and I am having trouble with the post command. Here is what I have so far:

 

# prompt user for credentials to infoblox
if sys.stdin.isatty():
print "Enter credentials to access infoblox"
username = raw_input("Username: ")
password = getpass.getpass("Password: ")
else:
username = sys.stdin.readline().rstrip()
password = sys.stdin.readline().rstrip()

# creates session to infoblox
session = requests.Session()
session.auth = (username, password)
session.verify = False
url = infobloxURL 

# silence http warnings
urllib3.disable_warnings()
 
ipAddress = "10.1.1.0"
hostName = "zocalo"

# searches the internal DNS to see if host exists 
response = session.get(url + 'record:host?name~=^%s.mydomain.net$' % hostName)

# if there is a response, response.content will contain a json object with the properties for that host
hostExists = "%s.mydomain.net" % (hostName) in response.content

if hostExists == True:
print ("The hostname already exists in infoblox. Please select a new hostname and try again")
sys.exit(1)
else:
print("Success! The host does not exist in infoblox")

# add a host into infoblox
host = { "name":"%s.mydomain.net$" % hostName,
"ipv4addrs":[
{
"ipv4addr":"%s" % ipAddress
}
]
}

response = session.post(url + 'record:host', data=host )

if response.status_code == 201:
print("successfully created a new host record in infoblox")
else:
print("failure, host not added to infoblox")
print (response.status_code)
here is a copy of the output I am getting in the terminal when running my code:
 
Enter credentials to access infoblox
Username: xxxxx
Password:
Success! The host does not exist in infoblox
{ "Error": "AdmConProtoError: List value expected for field: ipv4addrs",
"code": "Client.Ibap.Proto",
"text": "List value expected for field: ipv4addrs"
}
400
while i am able to perform a get function to search for existing hosts, I am getting a status_code of 400 when I am trying to add a new host using post. Can someone with python and infoblox experience help me figure out what I am doing wrong?

Re: Infoblox wapi python help with adding a new host to dns

Adviser
Posts: 181
8406     0

Hi,

 

It seems to be an issue with the way you are passing the parameters.

 

This is a sample pythin script I use to create host records using Python.

import requests
requests.packages.urllib3.disable_warnings()  # Disable SSL warnings in requests #
url = "https://grid-master/wapi/v2.11/record:host?_return_as_object=1"
payload = "{\"name\":\"host.info.com\",\"ipv4addrs\": [{\"ipv4addr\":\"172.26.1.20\"}]}"
headers = {'content-type': "application/json"}
response = requests.request("POST", url, auth=('admin', 'Infoblox'), data=payload, headers=headers, verify=False)
print(response.text)

Hope this helpful,

Regards,

Krishna Vasudevan

Re: Infoblox wapi python help with adding a new host to dns

New Member
Posts: 5
8406     0

Thanks I had figured out the problem but this is a good solution for anyone else who may come across this.

Re: Infoblox wapi python help with adding a new host to dns

Guru
Posts: 26
8406     0

Krishna, using this same methodology, can you post an example of searching for a host record, and then deleting it?

 

I can't seem to get the various commands (GET/POST/DELETE) to behave, and would appreciate seeing a common methodology.

 

Thanks!

Re: Infoblox wapi python help with adding a new host to dns

Adviser
Posts: 181
8406     0

Hi,

 

You can take a look the Python section of the Sample Codes in this document. Let me know if you need further assistance.

 

Thanks,

Krishna

Showing results for 
Search instead for 
Did you mean: 

Recommended for You