飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 3634|回复: 5

[Python] 20秒完成上千台服务器iLo信息的收集

  [复制链接]
  • TA的每日心情
    开心
    3 天前
  • 签到天数: 1315 天

    [LV.10]以坛为家III

    发表于 2020-5-23 07:07:37 | 显示全部楼层 |阅读模式
    本帖最后由 zyjsuper 于 2020-5-23 07:15 编辑

    客户这边的运维提出了一个需求,需要完成内网中所有HP服务器iLo口SBSN和KEY的收集,类似格式如下:

    IPSBSNKEYStatus
    OK或者Fail

    完成的python脚本如下,该脚本只收集可以ping通的服务器的iLo信息,如果没有iLo信息的ip地址标记为Fail。朋友们手里如果有更好的实现脚本,请不吝分享。
    [Python] 纯文本查看 复制代码
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    from urllib import request
    from xml.etree import ElementTree
    import xml.etree.cElementTree as ET
    import threading
    import subprocess
    import time
    from queue import Queue
    import os
    
    
    # 定义工作线程
    WORD_THREAD = 300  
    
    # 将需要 ping 的 ip 加入队列
    IP_QUEUE = Queue()
    for i in range(1,255):
        IP_QUEUE.put('10.10.1.'+str(i))
        IP_QUEUE.put('10.10.2.'+str(i))
        IP_QUEUE.put('10.10.3.'+str(i))
        IP_QUEUE.put('10.10.4.' + str(i))
        IP_QUEUE.put('10.10.5.' + str(i))
        IP_QUEUE.put('10.10.6.' + str(i))
        IP_QUEUE.put('10.10.7.' + str(i))
        IP_QUEUE.put('10.16.8.' + str(i))
    
    
    def getinfo(ip):
        url="http://"+ip+"/xmldata?item=cpqkey"
        web = request.urlopen(url,timeout=1)
        f = web.read()
        #解码
        data = ElementTree.XML(f.decode('utf-8'))
        #获取根节点
        tree = ET.ElementTree(data)
        root = tree.getroot()
        #根据索引和标签名获取所需要的值
        SBSN = root.find("SBSN").text
        KEY = root.find("KEY").text
        if(KEY):
            info=(ip + ", "  + SBSN.strip() + ", " + KEY.strip() + ",  " + "OK" + "\n")
            with open (filename,"a+") as file:
                file.write(info)
    def ping_ip():
        while not IP_QUEUE.empty():
            ip = IP_QUEUE.get()
            res = os.system('ping -c 2 -w 3 %s >/dev/null' % ip)  # Windows系统将 '-c' 替换成 '-n',根据网络情况调整,如果网络0延迟,可以改为 "-c 1 -w 1"                
           #res = subprocess.call('ping -n 1 -w 2 %s' % ip,stdout=subprocess.PIPE)             windows用这种方式
            # 打印运行结果
            if(res==0):
                try:
                    getinfo(ip)
                except:
                    info = (ip + ", " + ", " + ", "  + "Fail" + "\n")
                    with open(filename, "a+") as file:
                        file.write(info)
    if __name__ == '__main__':
        filename="iLOinfo"+time.strftime("%Y%m%d")+".csv"
        if(os.path.isfile(filename)):
            os.system("rm -f "+filename)                  #windows系统将 "rm -f"替换成"del /F"
        info = ("ip地址" + ", " + "SBSN" + ", " + "KEY" + ",  " + "状态" + "\n")
        with open(filename, "w") as file:
            file.write(info)
        threads = []
        start_time = time.time()
        for i in range(WORD_THREAD):
            thread = threading.Thread(target=ping_ip)
            thread.start()
            threads.append(thread)
    
    
        for thread in threads:
            thread.join()
    
    
        print("报告文件" + filename + "已经生成.")
        print('程序运行耗时:%s' % (time.time() - start_time))
    
    
    







    评分

    参与人数 1威望 +1 飘云币 +1 收起 理由
    此用户已注销 + 1 + 1 感谢发布原创作品,PYG有你更精彩!

    查看全部评分

    PYG19周年生日快乐!
  • TA的每日心情
    开心
    3 天前
  • 签到天数: 1404 天

    [LV.10]以坛为家III

    发表于 2020-5-23 16:35:56 | 显示全部楼层
    感谢大佬分享实战经验,支持分享。
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2023-7-21 10:06
  • 签到天数: 404 天

    [LV.9]以坛为家II

    发表于 2020-5-24 13:02:57 | 显示全部楼层
    20秒,好牛皮啊,还有更快的?
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-3 08:29
  • 签到天数: 621 天

    [LV.9]以坛为家II

    发表于 2020-5-27 09:31:52 | 显示全部楼层
    路过学习了,支持实战例子
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-14 04:37
  • 签到天数: 1685 天

    [LV.Master]伴坛终老

    发表于 2020-5-28 05:14:32 | 显示全部楼层
    非常感恩老师的分享!
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2024-4-12 20:47
  • 签到天数: 222 天

    [LV.7]常住居民III

    发表于 2021-10-16 10:23:31 | 显示全部楼层
    for i in range(1,255),255多了,254就行了
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表