Getting Started with the Python Library

Our python library is on Github! Feel free to take a look or even contribute here: https://github.com/Axonius/axonius_api_client 

We also have extensive documentation including some CLI videos located here: https://axonius-api-client.readthedocs.io/en/latest/index.html 

The python library provides both the library and the command line utility. In fact, our command line utility (axonshell) is just a command line interface on top of our library. If you are interested in how the python library is designed we have a really good article that talks about the anatomy of the python library located here: https://support.axonius.com/hc/en-us/community/posts/1500000976462-Anatomy-of-the-Axonius-Python-API-Client

To utilize our library, python must be installed on your system. If you do not already have it, you can download it from here: https://www.python.org/downloads/. Your OS might also have a better alternative method for installing python, feel free to use that as well.

After installation you should have the python package manager installed on your system called pip. This makes managing python packages very easy and ours is no different.

To install our library run the following command:

pip install axonius_api_client pyopenssl

In the future if you want to upgrade you can run:

pip install --upgrade axonius_api_client

Our python library and command line client were designed to follow a similar pattern. Meaning, with the python library to get the count of devices you would do:

client.devices.count()

And then in axonshell you would do:

axonshell devices count

We will talk more about axonshell in the next section.

To utilize either the python library or axonshell, you must specify how to connect to your Axonius server. We will need three pieces of information to set this up:

  • The server's URL.
  • Your users API key.
  • Your users API secret.

Your user's API key / secret can be found under your user's settings:

  • Click the gear under your profile image.
  • Click on the tab labelled "API Key".
  • You should see your "API Key" and "API Secret".

Once we have this information we can set up the python library to make use of it. We have three ways we can do this:

  • Add the information into a .env file. The python client will look first in the current working directory.
  • Add the information to the correct environment variables.
  • Pass the information into the Connect class.

We are going to take the first approach and create a .env file. We include a tool for this in axonshell:

axonshell tools write-config

You should end up with something that looks like the following in the file:

❯ cat .env
AX_URL='https://10.20.1.190/'
AX_KEY='6cwkNeek6r1_D0cwZUTy2dMzA4W_4l2WkN-yImtsLYY'
AX_SECRET='pN_LZm5N679n2hgm7EPjdp-RhOqpbHyNLswiys3FsoA'

Part of write-config's process validates the connection at the end. So long as there were not any errors displayed on the screen, we are ready to go.

The best way to get started is to take a look at the python libraries source code. We provide an examples directory in our package with numerous examples. One specifically was created to be used as a starting point for user scripts. It is called "script_base.py": https://github.com/Axonius/axonius_api_client/blob/master/axonius_api_client/examples/script_base.py.

This script creates the client and then loads the URL, API Key and API Secret from the default .env file. However, there are also commented lines that show how to pass the values in through the client_args of the Connect class. There are also lines that show how to control the scripts logging and all of the possible properties of the client class.

0

Comments

0 comments

Please sign in to leave a comment.

Didn't find what you were looking for?

New post