7 years ago
API/Script
Hi
I want to create device group named as "Fileserver" in /device/servers This is the first time I am running the script, so could you please check if my script is ok.
Also please le...
I want to create device groups which should read lines from text file and create a device group
Text file entries like as follows:
'{"name":"460-UNIVERSITY HALL - SIR GEORGE CURRIE HALL","parentId":150}'
'{"name":"452-AFTER SCHOOL CARE CENTRE (18 PARKWAY)","parentId":150}'
'{"name":"453-INDIAN OCEAN MARINE RESEARCH CENTRE (IOMRC)","parentId":150}'
In the code If insert
data = '{"name":"463-UNIVERSITY HALL - B HOUSE","parentId":150}' # works
but If I insert (where line is one line from the txt file)
data = line # does not work
Code details:
#!/bin/env python
import requests
import json
import hashlib
import base64
import time
import hmac
#Account Info
AccessId ='XXXXX'
AccessKey ='YYYYY'
Company = 'contoso'
#Open the txt file
with open('build.txt') as f:
for line in f:
#Request Info
httpVerb ='POST'
resourcePath = '/device/groups'
#data = '{"name":"463-UNIVERSITY HALL - B HOUSE","parentId":150}' # works
data = line # does not work
#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())
hmac = hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac.encode())
#Construct headers
#auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch
#headers = {'Content-Type':'application/json','Authorization':auth}
auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth}
#Make request
response = requests.post(url, data=data, headers=headers)