Thursday, February 11, 2021

A simple python script to insert objects into LogicMonitor

 I was playing around with he LogicMonitor API interface and wanted to try myhand at pyhton script to prompt for devices


Here's what I came up with 


$ cat addDeviceToLM.api2

#!/usr/bin/python


import requests

import json

import hashlib

import base64

import time

import hmac

import sys

print (sys.argv)


ipv4 = raw_input("Please enter device IPv4 address from lo0: ")

devname = raw_input("Please enter short device name e.g switch-1: ")


# echo back your data 

print ipv4

print devname


#Account Info for the API keys that you generate for the user

AccessId ='jJk7cu6nwKEfs85WYv547' # you access id

AccessKey ='h267(Zr{9A{Q99948a8{cK4n7P7G7}WZ(G^)63VV' # your access token 

Company = 'socpuppets'   # your potal name here




#Request Info: Add a service

httpVerb ='POST'

resourcePath ='/device/devices'


# POST DATA STRING SENT TO API

data = '{ "name": "'+ipv4+'", "displayName": "'+devname+'" , "preferredCollectorId":"122", "description": "'+devname+'" , "enableNetflow":"false" }'




#Construct URL

url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath


#Get current time in milliseconds

epoch = str(int(time.time() * 1000))


#Concatenate Request details

requestVars = httpVerb + epoch + data + resourcePath


#Construct signature

signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())


#Construct headers

auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch

headers = {'Content-Type':'application/json','Authorization':auth}


#Make request

response = requests.post(url, data=data, headers=headers)


#Print status and body of response

print 'Response Status:',response.status_code

print 'Response Body:',response.content




YMMV But LogicMonitor will give you a warning if the data is format incorrectly of it's a duplication





Ken Felix 

NSE ( network security expert) and Route/Switching Engineer
kfelix  -----a----t---- socpuppets ---dot---com
     ^      ^
=(  @  @ )=
         o

        /  \

No comments:

Post a Comment