Created
October 4, 2013 19:08
-
-
Save anthroprose/6831074 to your computer and use it in GitHub Desktop.
Python Script that calls the Zenoss JSON API to Refresh DNS for a DeviceClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib2 as http | |
import sys | |
import syslog | |
import urllib2 | |
import json | |
import base64 | |
from pprint import pprint | |
user = '' | |
pas = '' | |
deviceClass = '' | |
host = '' | |
try: | |
try: | |
from urlparse import urlparse | |
except ImportError: | |
from urllib.parse import urlparse | |
headers = { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json; charset=UTF-8', | |
'Authorization': "Basic {0}".format(base64.b64encode(user + ':' + pass)), | |
} | |
uri = 'https://' + host | |
path = '/zport/dmd/device_router' | |
target = urlparse(uri+path) | |
method = 'POST' | |
body = '{"action":"DeviceRouter","method":"getDevices","data":[{"params": { "deviceClass":"' + deviceClass + '" }, "sort": "name" }],"tid":1}' | |
h = http.Http(disable_ssl_certificate_validation=True) | |
response, content = h.request(target.geturl(), method, body, headers) | |
pprint(body) | |
pprint(response) | |
data = json.loads(content) | |
if len(data['result']['devices']) > 0: | |
for device in data['result']['devices']: | |
body = '{"action":"DeviceRouter","method":"resetIp","data":[{"uids":["' + device['uid'] + '"], "ip":"", "hashcheck":null }],"type":"rpc","tid":15}' | |
response, content = h.request(target.geturl(), method, body, headers) | |
pprint(body) | |
pprint(response) | |
device_data = json.loads(content) | |
except: | |
syslog.syslog(syslog.LOG_ERR, "Python Add-Device - Unhandled Exception: " + str(sys.exc_info())) | |
pprint(str(sys.exc_info())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment