python爬虫巡检思科集成管理控制器CIMC(Cisco Integrated Management Controller)

来源:互联网 发布:天津哪个淘宝城比较好 编辑:程序博客网 时间:2024/06/15 06:48

直接上干货,后续完善……

代码

# !/usr/bin/env python# -*- coding:utf-8 -*- # Date: 2017/9/1# Author: David Meng# Script name: cisco.py + cisco.json# Description: crawl CISCO health statusimport urllibimport urllib2import cookielibimport sslimport xml.dom.minidomimport timeimport osimport codecsimport jsondef ciscocheck(ip, user, passwd):    address, username, password = ip, user, passwd    # This file will be created temporarily in related path '.', and will be removed.    xmlfile = "cisco" + time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".xml"    deltempfile = "yes"    loginurl = "https://" + address + "/data/login/"    dataurl = "https://" + address + "/data?get%3DexternalLEDs%2CstorageHealth"    logout_url = "https://" + address + "/data/logout"    # DISABLE SSL    ssl._create_default_https_context = ssl._create_unverified_context    try:        # init a cookie        cookie = cookielib.CookieJar()        # create a opener using cookie        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))        # data to post        postdata = urllib.urlencode({            'user': username,            'password': password        })        # define a request, with url and data        req = urllib2.Request(url=loginurl, data=postdata)        # Open url and login        result = opener.open(req)        # Open data url, get data        xmldata = opener.open(dataurl)        # wirte data to file        f = file(xmlfile, 'w')        f.writelines(xmldata.read())    except Exception, e:        print "[ErrorMsg]: %s" % e    finally:        # logout session        logout = opener.open(logout_url)        f.close()    try:        # parse xml data        DOMTree = xml.dom.minidom.parse(xmlfile)        collection = DOMTree.documentElement        sep = "=" * 40        print sep + ip + " start" + sep        health = collection.getElementsByTagName("health")[0].childNodes[0].data        print ("===== Health: %s =====" % health)        status = collection.getElementsByTagName("status")[0].childNodes[0].data        print ("===== Status: %s =====" % status)        leds = collection.getElementsByTagName("led")        for led in leds:            print "-----LED %s-----" % led.getElementsByTagName('id')[0].childNodes[0].data            type = led.getElementsByTagName('id')[0]            print "|id: %s |" % type.childNodes[0].data,            format = led.getElementsByTagName('ledName')[0]            print "ledName: %s |" % format.childNodes[0].data,            rating = led.getElementsByTagName('ledState')[0]            print "ledState: %s |" % rating.childNodes[0].data,            description = led.getElementsByTagName('ledColor')[0]            print "ledColor: %s |" % description.childNodes[0].data        print sep + ip + "   end" + sep    except Exception, e:        print "[ErrorMsg]: %s" % e    finally:        if (deltempfile == "yes"):            os.remove(xmlfile)if __name__ == '__main__':    fname = os.path.join("cisco.json")    if os.path.exists(fname):        f = codecs.open(fname, mode='r', encoding='utf-8')        iplist = json.load(f)        f.close()        for jj in iplist:            ciscocheck(**jj)
# cisco.json[  {    "ip": "192.168.1.111",    "user": "username",    "passwd": "password"  },  {    "ip": "192.168.1.112",    "user": "username",    "passwd": "password"  }]
原创粉丝点击