- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 01:49 PM - edited 01-24-2023 02:11 PM
I am trying to find the device ID in our DNAC server and having issues. We have some devices in DNAC by IP address and some by dnsname. We have a python script that adds/deletes devices via IP address on our other NMS softwares and I'd like to add the ability for it to call out to DNAC to do the same. But my problem is that when I try to input my device hostname in my api url filter, I get an error and I'm not understanding why.
def getHost(ip):
"""
This method returns the host name for a
given IP address
"""
try:
data = socket.gethostbyaddr(ip)
host = repr(data[0])
return host
except Exception:
# fail gracefully
return False
device_name = getHost(DEVICE_IP)
dn = str(device_name)
#Get Auth Token
response = requests.post(BASE_URL + AUTH_URL, auth=HTTPBasicAuth(USERNAME, PASSWORD))
token = response.json()['Token']
headers = {'X-Auth-Token' : token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
#Filter by hostname
query_string_params = {'hostname': dn}
response = requests.get(BASE_URL + DEVICES_URL, headers=headers, params=query_string_params)
device_id = response.json()['response'][0]['id']
print(device_id)
Now if I replace the "dn" in my query_string_params with the actual device's name, it works just fine. Or if I change the dn = 'test_device.mynetwork.com' it also works just fine. It seems to be something with the output of my getHost function?
Solved! Go to Solution.
- Labels:
-
Cisco DNA
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 12:42 AM - edited 01-25-2023 12:46 AM
Hi, don't use repr(), cause this will return the result in single quotes. In your function just do host = data[0]
In [8]: data = socket.gethostbyaddr("8.8.8.8")
In [9]: print(repr(data[0]))
'dns.google'
In [10]: print(data[0])
dns.google
HTH
Marcel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 12:42 AM - edited 01-25-2023 12:46 AM
Hi, don't use repr(), cause this will return the result in single quotes. In your function just do host = data[0]
In [8]: data = socket.gethostbyaddr("8.8.8.8")
In [9]: print(repr(data[0]))
'dns.google'
In [10]: print(data[0])
dns.google
HTH
Marcel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 06:09 AM
That was it! Thank you! I'm still learning. what does the "repr" do?
this is what I changed my function to.
def getHost(ip):
"""
This method returns the host name for a
given IP address
"""
try:
data = socket.gethostbyaddr(ip)
host = data[0]
return host
except Exception:
# fail gracefully
return False
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 06:19 AM
You're welcome. Regarding the repr-function: see https://www.tutorialsteacher.com/python/repr-method
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 06:39 AM
Thank you so much! I'm excited to be learning python and appreciate the honest reply without being snarky like most people! It's very helpful and encouraging.
