analytics

Automatically create patch reports from DSM Management Suite with Python

Automatically create patch reports from DSM Management Suite with Python

Automatically create patch reports from DSM Management Suite with Python

Introduction

The heart of the DSM Management Suite is the backend or the Web API. The backend processes all data that are transmitted by the Client Sync Script. Both the Windows Dashboard and the Web Dashboard do nothing else than retrieve this data and graph it for you. If you need more individual reports or want to generate reports in a time-controlled manner, you can do this at any time by simply sending your evaluations yourself to the back-end of DSM Management Suite. In this article, I will tell you how you can automatically create patch reports from DSM Management Suite using Python

Why Python?

You can send and receive data to your backend in any programming language. However, Python is very easy to learn and easy to read. In addition, the Python has very good libraries for data analysis and even for machine learning. Python runs on both OSX, Windows and Linux, making it the ideal programming language for all operating systems

We determine all patch data with a Python script

In this example we would like to log in to the web API (backend) with the “demo” user and then query patch data. This data could then be evaluated by you or processed further. In this example we will save the data as a CSV file. We use Python 3.7 . Other versions work, but may need a different syntax. How to install Python can be found on the official Python website

Step 1: We install all the necessary dependencies

We use 3 extensions in our script:

  • Requests
  • Json
  • CSV

We can easily install these extensions using the command line and “pip” the Python package manager

pip install requests
pip install json
pip install csv

As soon as the installation is finished, we start with the actual Python Script

Step 2: Python DSM Management Suite - authentication on the server

Authentication on the server or on the API of DSM Management Suite takes place using a so-called JSON Web Token, or JWT for short. We then have to send this token to the server with every request so that we are authenticated. We receive this token as a “response” from the backend as soon as we send our login data to the login URL from the server. The login URL from the demo server is:

https://api.dsm-management-suite.de/api/Auth/login

Adjust your URL according to your installation. We use a class called “Requests” to send our user data to the server. We have to import this class first. We are already doing the same with our Json Library

import requests
import json

auth_url = "https://api.dsm-management-suite.de/api/Auth/login"

In the next step we define our login data which we send to the server

data = {
   "username" : "demo",
   "password": "demo1234"
}

auth_request = requests.post(auth_url, auth=(''), verify=False, json=data)

If the login was successful, we get back as status code 200. So we check whether the login was successful and continue the program if the login worked.

if auth_request.status_code==200:
   #hier war der login erfolgreich
else:
   print('error Status code: ' + auth_request.status_code)

If the login was successful, we get the token back from the server in the request text (body). This token is formatted as json. We now store this in a variable and use this token to get the patch data with a get request. For this we use the URL to determine the patch data

patch_url = "https://api.dsm-management-suite.de/api/PatchData"
token = json.loads(auth_request.text)
#print(token["token"])
bearer_token = 'bearer ' + token["token"]
patch_request = requests.get(patch_url, headers={'Authorization': bearer_token})
patchData = json.loads(patch_request.text)

Now we just want to save all the data received as a CSV file. This just works with the CSV class. We import these at the beginning of the file where we have also defined the other imports.

import csv

Now we can save each patch record in a CSV file

with open('patchData.csv', mode='w') as save_file:
writer = csv.writer(save_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['id', 'computer', 'patch', 'compliance', 'foundDate', 'fixDate', 'createdAt'])
for patch in patchData:
   writer.writerow([patch['id'], patch['computer'], patch['patch'], patch['compliance'], patch['foundDate'], patch['fixDate'], patch['createdAt']])

The complete source code:

import requests
import json
import csv

auth_url = "https://api.dsm-management-suite.de/api/Auth/login"
patch_url = "https://api.dsm-management-suite.de/api/PatchData"

data = {
   "username" : "demo",
   "password": "demo1234"
}

auth_request = requests.post(auth_url, auth=(''), verify=False, json=data)

if auth_request.status_code==200:
   token = json.loads(auth_request.text)
   print(token["token"])
   bearer_token = 'bearer ' + token["token"]
   patch_request = requests.get(patch_url, headers={'Authorization': bearer_token})
   patchData = json.loads(patch_request.text)

   with open('patchData.csv', mode='w') as save_file:
      writer = csv.writer(save_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
      writer.writerow(['id', 'computer', 'patch', 'compliance', 'foundDate', 'fixDate', 'createdAt'])
      for patch in patchData:
         writer.writerow([patch['id'], patch['computer'], patch['patch'], patch['compliance'], patch['foundDate'], patch['fixDate'], patch['createdAt']])
else:
   print('error Status code: ' + auth_request.status_code)

On my software development blog I will publish even more diverse Ivanti DSM and DSM Management Suite scripts. Just take a look. Do you need an individual reporting script? Get help with Python programming

are can be quite complicated when installing the application for the first time. Since there are quite a few blocks in the application that can run differently, it is difficult to create a simple setup routine here. As an Patreon Edition user you have access to the Help Center and you can ask me directly for support. Community users must describe their concerns in github. In both cases I will help you as soon as possible.

Did you like the post?

Become a Patreon and support my work. As a thank you, you will receive exclusive articles, be listed as a sponsor for all my open source projects (do-follow backlink) and get access to exclusive articles.
Don't fancy Patreon? You can also easily support me if you like this article or share it on social media. Thank you!