cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
8026
Views
5
Helpful
11
Replies

How can I make this script to run?

Hello I've recently started to learn about devnet so I'm starting with easy things like how to code or how to manage Cisco DNA Center but I've got a difficult start when I'm trying to compile this script I always get the message of WARNING dnac_config is not a module I know it's a python file but I don't know how to make it work 

 

Just in case I'm going to share the code:

 

import requests   # We use Python external "requests" module to do HTTP query
import dnac_config # DNA-C IP is assigned in dnac_config.py
from requests.auth import HTTPBasicAuth


def get_auth_token():
"""
Building out Auth request. Using requests.post to make a call to the Auth Endpoint
"""
url = 'https://sandboxdnac.cisco.com/dna/system/api/v1/auth/token' # Endpoint URL
resp = requests.post(url, auth=HTTPBasicAuth(DNAC_USER, DNAC_PASSWORD)) # Make the POST Request
token = resp.json()['Token'] # Retrieve the Token from the returned JSON
print("Token Retrieved: {}".format(token)) # Print out the Token
return token # Create a return statement to send the token back for later use

if __name__ == "__main__":
get_auth_token()

I've been using PyCharm for easy examples about Postman but where I'm starting to begin my journey it's almost impossible (Also I don't know how to use Cisco DNA Center). I will be so grateful if you answer me correctly to get an output about all of this.

 

Greetings,

A newbie

 

 

1 Accepted Solution

Accepted Solutions

Just run script in command line. I assume you already have python installed in your windows.

 

Please refer to  How To Setup Your Own Computer 

on the top of this learning lab. 

https://developer.cisco.com/learning/lab/dnav3-dnac-nbapi-hello-world/step/1

You can also follow this learning lab.

View solution in original post

11 Replies 11

yawming
Cisco Employee
Cisco Employee

You will need a file named dnac_config.py

 

I think DNAC_USER, DNAC_PASSWORD are assigned over there.

 

The DNAC using in this script is based on the following DevNet sandbox.

 

 

 https://devnetsandbox.cisco.com/RM/Diagram/Index/471eb739-323e-4805-b2a6-d0ec813dc8fc?diagramType=Topology

Thank you for your answer!

 

I know that I need a file named dnac_config.py because that's why you import dnac_config all the settings for DNAC, DNAC_USERNAME and DNAC_PASSWORD without this file we can't work.

 

I'm thinking that maybe I don't know how to use DNA Center properly and that I'm misunderstanding/ not finding the key to do these kind of exercises.

 

Also that link says that because you have made a reservation I can't

can you attach dnac_config.py ?

are you using mac or Windows ? I will run it through to show you.

It would be helpful at least to show me a capture of this working on DNA Center and Python script (what program are you using exactly).

 

I'm using Windows 10 at the latest version/updates

Just run script in command line. I assume you already have python installed in your windows.

 

Please refer to  How To Setup Your Own Computer 

on the top of this learning lab. 

https://developer.cisco.com/learning/lab/dnav3-dnac-nbapi-hello-world/step/1

You can also follow this learning lab.

Thank you I realized that I needed to make a link between those two files with the dnac_config.DNAC_USER and DNAC_PASSWORD

 

What I really don't understand is why from the file dnac_config they import DNAC and DNAC_PORT

 

 

Sometime people put the frequent changed parameters in other file so they don't need to modify source code just because they need to change username, password, ip....etc.

 

import other file just the way python include (reference to) other file.

Yup I've downloaded a dnac_config just to make sure what variables are used inside and it's something like this

 

"""
Copyright (c) 2018 Cisco and/or its affiliates.

This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.0 (the "License"). You may obtain a copy of the
License at

https://developer.cisco.com/docs/licenses

All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
"""
@author Washim Bari
"""
"""
Sample Configuration
DNAC_IP = "1.1.1.1"
DNAC_PORT = 443
USERNAME = "username"
PASSWORD = "password"
VERSION = "v1"
DEVICE_IP = "2.2.2.2"
PRODUCT_FAMILY = "Routers"
TEMPLATE_NAME = "MyTemplate"
PROCESS_NAME = "dbm"
FTP_SERVER = "3.3.3.3"
FTP_USERNAME = "ftp-username"
FTP_PASSWORD = "ftp-password"
QUERY_INTERVAL = "1800"
"""
DNAC_IP = "Your DNA Center Cluster IP Address"
DNAC_PORT = 443
USERNAME = "Your DNA Center Username"
PASSWORD = "Your DNA Center Password"
VERSION = "v1"
DEVICE_IP = "Device IP Address"
PRODUCT_FAMILY = "Device Family - Any one from the list {Routers, Switches}"
TEMPLATE_NAME = "Any Name for the Template that you want to deploy"
PROCESS_NAME = "IOS process name - Any one from the list but not limited to {smand, dbm, wcm, pman, fman, hman, vman}"
FTP_SERVER = "Your FTP Server IP Address"
FTP_USERNAME = "Your FTP Server Username"
FTP_PASSWORD = "Your FTP Server Password"
QUERY_INTERVAL = "Time Interval in seconds for EEM run; min 300 to max 604800"

 

Most important variables are: 

DNAC_IP = "Your DNA Center Cluster IP Address"
DNAC_PORT = 443
USERNAME = "Your DNA Center Username"
PASSWORD = "Your DNA Center Password"

but not variables like DNA created from nowhere

here is my dnac_config.py:

 

DNAC_USER = "devnetuser"
DNAC_PASSWORD = "Cisco123!"

 

 

 

and here is my script: (get_auth.py):

 

 

import requests # We use Python external "requests" module to do HTTP query
import dnac_config # DNA-C IP is assigned in dnac_config.py

from requests.auth import HTTPBasicAuth


def get_auth_token():
"""
Building out Auth request. Using requests.post to make a call to the Auth Endpoint
"""
url = 'https://sandboxdnac.cisco.com/dna/system/api/v1/auth/token' # Endpoint URL
resp = requests.post(url, auth=HTTPBasicAuth(dnac_config.DNAC_USER, dnac_config.DNAC_PASSWORD)) # Make the POST Request
token = resp.json()['Token'] # Retrieve the Token from the returned JSON
print("Token Retrieved: {}".format(token)) # Print out the Token
return token # Create a return statement to send the token back for later use

if __name__ == "__main__":
get_auth_token()

 

 

 

 

I am using Mac so I run it as 

 

Mochi$python get_auth.py

 

and this is the output

 

Token Retrieved: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1YmQ5OGQzNWIyYmVhMDAwNGMzZWM5YmYiLCJhdXRoU291cmNlIjoiaW50ZXJuYWwiLCJ0ZW5hbnROYW1lIjoiVE5UMCIsInJvbGVzIjpbIjViZDM2MzRiYjJiZWEwMDA0YzNlYmI1YSJdLCJ0ZW5hbnRJZCI6IjViZDM2MzRhYjJiZWEwMDA0YzNlYmI1OCIsImV4cCI6MTU2MjAyMDU4NSwidXNlcm5hbWUiOiJkZXZuZXR1c2VyIn0.mryjlXPOoBvL_U55pC65WuSM98kheI42AqTuPHyqcfQ

Mochi$

 

I know this is going too much but could you make some screenshots about how the process it's done so I could know the entire process? Because I'm still confused about DNA Center and how to use it (there is no tutorial on how to make simple things like this ones)

Thank you!

I am using the Devnet DEVASC VM. it has VS Code. I am able to runthis script with no errors, but do not get a printed output. This is what I have on my VM

Dnac_config.py

DNAC_USER = "devnetuser"

DNAC_PASSWORD = "Cisco123!"

Simple_auth.py

import requests

import dnac_config

from requests.auth import HTTPBasicAuth

#from dnac_config import DNAC_USER, DNAC_PASSWORD

def get_auth_token():

    """

    Building out Auth request. Using requests.post to make a call to the Auth Endpoint

    """

    url = 'https://sandboxdnac.cisco.com/dna/system/api/v1/auth/token'       # Endpoint URL

    resp = requests.post(url, auth=HTTPBasicAuth(dna_config.DNAC_USER, dna_config.DNAC_PASSWORD))  # Make the POST Request

    token = resp.json()['Token']    # Retrieve the Token from the returned JSON

    print("Token Retrieved: {}".format(token))  # Print out the Token

    return token    # Create a return statement tif __name__ == "__main__":

    get_auth_token()

    if __name__ == "__main__":

        get_auth_token()

I run with the following:

devasc@labvm:~$ python3 /home/devasc/Simple_auth.py

devasc@labvm:~$

I thought I would get the token printed. Any suggestions?

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Innovations in Cisco Full Stack Observability - A new webinar from Cisco