您好,登錄后才能下訂單哦!
# -*- coding:utf-8 -*- import json import urllib2 from urllib2 import URLError #import sys #import xlrd class ZabbixTools: def __init__(self): self.url = 'http://10.11.0.215/zabbix/api_jsonrpc.php'] self.header = {"Content-Type": "application/json"} # 登陸驗證 def user_login(self): data = json.dumps({ "jsonrpc": "2.0", "method": "user.login", "params": { "user": 'admin', "password": 'zabbix' }, "id": 0 }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: print "Auth Failed, please Check your name and password:", e.code else: response = json.loads(result.read()) result.close() self.authID = response['result'] return self.authID # 獲取主機 def host_get(self): data = json.dumps({ "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["hostid", "host"], "selectInterfaces": ["interfaceid", "ip"] }, "auth": self.user_login(), "id": 1, }) request = urllib2.Request(self.url, data) for key in self.header: request.add_header(key, self.header[key]) try: result = urllib2.urlopen(request) except URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server could not fulfill the request.' print 'Error code: ', e.code else: response = json.loads(result.read()) result.close() lens = len(response['result']) if lens > 0: return response['result'] else: return "" #################################################################################### # -*- coding: utf-8 -*- import xlrd import xlwt #xlutils結(jié)合xlrd可以達到修改excel文件目的 from xlutils.copy import copy from openpyxl import Workbook from openpyxl import load_workbook from openpyxl.writer.excel import ExcelWriter ''' 1.類的內(nèi)部調(diào)用:self.<方法名>(參數(shù)列表)。 2.在類的外部調(diào)用:<實例名>.<方法名>(參數(shù)列表)。 注意:以上兩種調(diào)用方法中,提供的參數(shù)列表中都不用包括self。 ''' class ExcelOperation(object): ''' 設(shè)置默認參數(shù):rowsize colsize ,創(chuàng)建對象時可以寫、可不寫默認參數(shù) ''' def __init__(self, filename, rowsize=0, colsize=0): self.filename = filename self.rowsize = rowsize self.colsize = colsize def open_excel(self): return xlrd.open_workbook(self.filename) ''' table = data.sheets()[0] #通過索引順序獲取 table = data.sheet_by_index(0) #通過索引順序獲取 table = data.sheet_by_name(u'Sheet1')#通過名稱獲取 ''' def read_excel(self): workbook = self.open_excel() sheet = workbook.sheet_by_index(0) nrows = sheet.nrows ncols = sheet.ncols data = [] for loop in range(0,nrows): data.append(sheet.row_values(loop)) return data def write_excel(self): wbk = xlwt.Workbook() sheet = wbk.add_sheet('sheet1') # 第0行第一列 寫入內(nèi)容 sheet.write(0,1,'test ') wbk.save('test.xls') def read_write_excel(self): ''' 4.openpyxl可以對excel文件進行讀寫操作 from openpyxl import Workbook from openpyxl import load_workbook from openpyxl.writer.excel import ExcelWriter workbook_ = load_workbook(u"新歌檢索失敗1477881109469.xlsx") sheetnames =workbook_.get_sheet_names() #獲得表單名字 print sheetnames sheet = workbook_.get_sheet_by_name(sheetnames[0]) print sheet.cell(row=3,column=3).value sheet['A1'] = '47' workbook_.save(u"新歌檢索失敗1477881109469_new.xlsx") wb = Workbook() ws = wb.active ws['A1'] = 4 wb.save("新歌檢索失敗.xlsx") :return: ''' workbook_ = load_workbook(u'test.xlsx') sheetnames = workbook_.get_sheet_names() sheet = workbook_.get_sheet_by_name(sheetnames[0]) print sheet.cell(row=3, column=3).value ################################################################################## # -*- coding: utf-8 -*- from testZabbixApi import ZabbixTools from pyExcel import ExcelOperation # 程序的入口 if __name__ == "__main__": #實例化excel對象 tab = ExcelOperation('主機列表.xlsx') data = tab.read_excel() dirc = {'item{}'.format(i): x for i, x in enumerate(data)} #print dirc['item45'][6] # 實例化ZabbixTools對象 test = ZabbixTools() result = test.host_get() for key in dirc: flag = 'F' for i in range(len(result)): if dirc[key][6].strip() == result[i]["interfaces"][0]["ip"]: flag = 'T' if flag == 'T': #print(dirc[key][6] + " zabbix") pass else: print(dirc[key][6] + "not add zabbix")
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。