Learn How We Can Help You Keep Teleworkers Protected During the COVID-19 Crisis

API Examples

Reply
Highlighted
Accepted Solution

Infoblox wapi python help with adding a new host to dns

[ Edited ]
Member
Posts: 2
852     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?
Highlighted

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

Adviser
Posts: 136
853     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

Highlighted

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

Member
Posts: 2
853     0

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

Showing results for 
Search instead for 
Do you mean 

Recommended for You