Skip to content

Instantly share code, notes, and snippets.

@anthroprose
Created October 4, 2013 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthroprose/6831074 to your computer and use it in GitHub Desktop.
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
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