Creating automation script to fetch assets or devices from Claroty

About this task

An automation script is created for getting assets and devices details from Claroty.

Follow the steps below to create the automation script:

Procedure

  1. Open the Automation Scripts application.
  2. On the navigation panel, click Create and select Script with Action Launch Point : Step 1 of 3, and enter data in the fields as follows:
    • Launch Point: Specify the launch point name, for example, DISCOVERY.
    • Object: Search for the ASSET object and select it.
    • Action: Specify the Action name same as the name given for Launch Point, for example, DISCOVERY.
    • Check the checkbox for Active? and click Next.
  3. On the Create Script with Action Launch Point : Step 2 of 3 pop-up, enter the details in the fields as follows:
    • Script: Specify as Jython
    • Script Language: Search and select jython
    • Log level: Search and select ERROR
    • Click Next.
  4. On the Create Script with Action Launch Point : Step 3 of 3. In the Script box, enter the following script and click Create.
    Note: The below script is a sample and for reference only, update the script as per your custom field ID's and other changes as necessary.
    from java.lang import System, Class, String, StringBuffer
    from java.io import BufferedReader, InputStreamReader
    from java.net import HttpURLConnection, URL
    from java.lang import StringBuilder
    from java.util import HashMap
    from psdi.iface.router import HTTPHandler
    from com.ibm.json.java import JSON, JSONObject,JSONArray
    from psdi.util.logging import MXLoggerFactory
    from org.apache.http.entity import StringEntity
    from com.ibm.json.java import JSONObject, JSONArray
    from psdi.iface.router import Router
    from psdi.iface.mic import EndPointCache
    from java.io import BufferedReader, IOException, InputStreamReader
    from java.lang import System, Class, String, StringBuffer
    from java.nio.charset import Charset
    from java.util import Date, Properties, List, ArrayList ,HashMap , Calendar
    from psdi.app.ticket import TicketRemote
    
    from org.apache.commons.codec.binary import Base64
    from org.apache.http import HttpEntity, HttpHeaders, HttpResponse, HttpVersion
    from org.apache.http.client import ClientProtocolException, HttpClient
    from org.apache.http.client.entity import UrlEncodedFormEntity
    from org.apache.http.client.methods import HttpPost
    from org.apache.http.entity import StringEntity
    from org.apache.http.impl.client import DefaultHttpClient
    from org.apache.http.message import BasicNameValuePair
    from org.apache.http.params import BasicHttpParams, HttpParams, HttpProtocolParamBean
    
    from psdi.mbo import Mbo, MboRemote, MboSet, MboSetRemote,MboConstants,SqlFormat
    from psdi.mbo import MboSetEnumeration
    
    from psdi.security import UserInfo
    from psdi.server import MXServer
    
    def generateJSON():
      filterObj = {
      "filter_by": {
        "operation": "and",
        "operands": [
          {
            "field": "device_category",
            "operation": "in",
            "value": [
              "IoT",
              "Medical"
             
            ]
          }
        ]
       
      },
      "limit": 100,
      "offset": 0,
      "fields": [
         "device_category",
        "device_subcategory",
        "device_type",
        "uid",
        "asset_id",
        "mac_list",
        "ip_list",
        "device_type_family",
        "model",
        "local_name",
        "os_name",
        "machine_type",
        "last_domain_user_activity",
        "last_scan_time",
        "is_online"
      ]
         
      }
    
      return filterObj
     
    jsonStr = generateJSON()
     
    # method for http POST using url, JSON body and token with bearer authorization
    def httpPost(uri, jsonstring):
        # get http parameters
        params = BasicHttpParams()
        paramsBean = HttpProtocolParamBean(params)  
        # get http body entities
        entity = StringEntity(jsonstring)
    
        # get client, http headers and request
        client = DefaultHttpClient()
        request = HttpPost(uri)
        request.setParams(params)
        request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json")
        request.addHeader('Authorization',mbo.getString("TOKEN"))
        request.setEntity(entity)
    
        # get client response
        response = client.execute(request)
        # return JSON response and reference number
        status = response.getStatusLine().getStatusCode()
        obj = JSONObject.parse(response.getEntity().getContent())
        return obj
    
    
    jsdata = str(jsonStr).replace("\'", "\"")
    url = mbo.getString("url")
    log = httpPost(url, jsdata)
    
    
    # pass claroty api response to keymapper script
    varx = HashMap()
    varx.put("respData",log.toString())
    service.invokeScript("KEYMAPPER",varx)
    
  5. Save your changes.