cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1320
Views
15
Helpful
4
Replies

DNAC API trying to retrieve device id by hostname by doing dns lookup

ab23
Level 1
Level 1

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. 

AUTH_URL = '/dna/system/api/v1/auth/token'
DEVICES_URL = '/dna/intent/api/v1/network-device'
DEVICES_COUNT_URL = '/dna/intent/api/v1/network-device/count'
DEVICES_BY_ID_URL = '/dna/intent/api/v1/network-device/'
DEVICE_IP = input('What is the device IP? ')

 

 

 

 

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? 

1 Accepted Solution

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

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

View solution in original post

4 Replies 4

Marcel Zehnder
Spotlight
Spotlight

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

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

You're welcome. Regarding the repr-function: see https://www.tutorialsteacher.com/python/repr-method

 

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.