欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    机器信息采集脚本python.docx

    • 资源ID:17100191       资源大小:182.70KB        全文页数:13页
    • 资源格式: DOCX        下载积分:5金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要5金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    机器信息采集脚本python.docx

    1、机器信息采集脚本python机器信息采集脚本(python)目录一、做什么的? 2二、怎么做? 4(1)、依赖的库 4(2)、命令行解析模块 optparse 4(3)windows系统信息 4(4)Linux系统信息 74.1 cpu信息 74.2 network信息 94.3 存储信息 10(5)数据库信息 11(6)已经测试环境 117.SunOS sunv440 不支持 12标签:脚本,python,系统管理员,命令行,解析器,操作系统源代码位置: (希望大家能多多star收藏和fork改进程序)一、做什么的?(1)主要用于采集服务器的相关信息,提供两种交互方式,一种是将服务器信息汇总

    2、至xml文件中,便于软件开发者使用(图1);另一种方式通过命令行交互的方式,通过输入参数信息查询相关信息(图2)。 图1图2(2)主要功能 uname : 操作系统版本 、型号 、处理器、机器名等 CPU :型号 、数量 、使用率等 network : 网卡信息、MAC、IP、子网掩码、网络I/O等 memory : 内存大小、使用率、可用大小等 存储 : 类型、大小、使用情况、磁盘I/O等 数据库版本和安装路径:Mysql、oracle、sqlserver、sybase、db2二、怎么做?(1)、依赖的库psutil库 下载地址:https:/pypi.python.org/pypi/psu

    3、tilwindows系统需要安装WMI库 下载地址:https:/pypi.python.org/pypi/WMI/#downloads 这个库在安装之前要安装pywin32(2)、命令行解析模块 optparse功能强大,且易于使用,可以方便地生成标准的、符合Unix/Posix规范的命令行说明。文档说明中文翻译:(3)windows系统信息WMI 最初于1998年作为一个附加组件与 Windows NT 4.0 Service Pack 4 一起发行,是内置在Windows 2000、 Windows XP和Windows Server 2003 系列操作系统中核心的管理支持技术。基于由

    4、Distributed Management Task Force (DMTF) 所监督的业界标准,WMI是一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的Windows资源。大多用户习惯于使用众多的图形化管理工 具来管理Windows资源,在WMI之前这些工具都是通过 Win32应用程序编程接口(Application ProgrammingInterfaces,API)来访问和管理Windows资源的。只要你熟悉系统编程你就知道API有多么重要。但是大多数脚本 语言都不能直接调用Win32 API,WMI的出现使得系统管理员可以通过一种简便的方法即利用常见的脚本语言实现常用的

    5、系统管理任务。利用WMI需要和脚本如WSH和VBScript结合起来,可以实现的功能大家可以看微软的MSDN文档。因此基本上windows系统能表现出来,你都可以通过这个模块获取,具体的可参考下面代码:#!/usr/bin/env python# -*- coding: utf-8 -*-try:import wmiimport win32apiexcept ImportError:wmi = Noneimport sys,platformimport subprocessimport osdef get_system_info(c,sys):if sys = Windows:for sys

    6、in c.Win32_OperatingSystem():print Version :t%s % sys.Caption.encode( GBK)print Vernum :t%s % sys.BuildNumberdef get_memory_info(c,sys):if sys = Windows:for mem in c.Win32_MemoryArray():print t + str(mem.Caption) + t + str(mem.Name)cs = c.Win32_ComputerSystem()pfu = c.Win32_PageFileUsage()MemTotal =

    7、 int(cs 0.TotalPhysicalMemory)/ 1024/1024print TotalPhysicalMemory : + t + str(MemTotal) + M#tmpdict MemFree = int( sys0.FreePhysicalMemory)/1024SwapTotal = int(pfu 0.AllocatedBaseSize)print SwapTotal : + t + str(SwapTotal) + M#tmpdict SwapFree = int( pfu0.AllocatedBaseSize - pfu0.CurrentUsage)def g

    8、et_disk_info(c,sys,infolist):if sys = Windows:tmpdict = dict()tmplist=list()for physical_disk in c.Win32_DiskDrive():if physical_disk.Size:tmpdict disk=str(physical_disk.Caption)tmpdictsize =str(long(physical_disk.Size)/1024/1024/1024)+Gtmpdict dev = str(physical_disk.MediaType)tmplist.append(tmpdic

    9、t)print tmpdictdev +:t+tmpdictdisk + :t + tmpdictsize infolist.append(tmplist) def get_cpu_info(c,sys,infolist):if sys = Windows:tmplist=list()tmpdict=dict()tmpdict CpuCores = 0for cpu in c.Win32_Processor(): tmpdict model name = cpu.Nametry:tmpdict CpuCores = cpu.NumberOfCoresexcept:tmpdict CpuCore

    10、s += 1tmpdict CpuClock = cpu.MaxClockSpeed print CpuType :t + str(tmpdictmodel name)print CpuCores :t + str(tmpdictCpuCores)tmplist.append(tmpdict)#infolist.append(tmplist)return tmplistdef get_network_info(c,sys,infolist):if sys = Windows:tmplist=list()for interface in c.Win32_NetworkAdapterConfigu

    11、ration (IPEnabled=1):tmpdict=dict()tmpdict Description = interface.Descriptiontmpdict IPAddress = interface.IPAddress0 tmpdict IPSubnet = interface.IPSubnet0 tmpdict MAC = interface.MACAddresstmplist.append(tmpdict)for i in tmplist:print iDescriptionprint t + MAC : + t + iMAC print t + IPAddress : +

    12、 t + iIPAddress print t + IPSubnet : + t + iIPSubnet infolist.append(tmplist)for interfacePerfTCP in c.Win32_PerfRawData_Tcpip_TCPv4():print TCP Connect :t + str(interfacePerfTCP.ConnectionsEstablished)def get_Proceess_cmd(c,process_name):cmd = for process in c.Win32_Process():temp = unicode(process

    13、.CommandLine)name = process.Nameif name.find(process_name) = 0:cmd = tempreturn cmddef get_info(cmd):p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell= False)return municate() 0.split( n)0 if _name_ = _main_:sys = platform.system()infolist = list()c = wmi.WMI ()(4)Linux系统信息4.1 cpu信息在linux系统中,提

    14、供了proc文件系统显示系统的软硬件信息.CPU的信息在启动的过程中被装载到虚拟目录/proc下的cpuinfo文件中,我们可以通过cat /proc/cpuinfo查看一下:这样我们只需要通过分析这个文件来获取cpu的相关信息,代码如下:ef get_cpu_info(sys,infolist):cpuinfo=dict()procinfo=dict()tempdict = dict()templist = list()if sys = Linux: Return the information in /proc/cpuinfoas a dictionary in the following

    15、 format:cpu_infoproc0=.cpu_infoproc1=.nprocs = 0f = file( /proc/cpuinfo,r)while True :line = f.readline()if len(line) = 0:breakif not line.strip():# end of one processorcpuinfo proc %s % nprocs = procinfonprocs=nprocs+ 1# Resetprocinfo=dict()else:if len(line.split( :) = 2 :temp1 = line.split( :)0 .s

    16、trip()temp2 = line.split( :)1 .strip()tempdicttemp1 = temp2print temp1+ : +temp2else:procinfoline.split( :)0 .strip() = templist.append(tempdict)# infolist.append(tempdict)return templist4.2 network信息想实现一个类似ifconfig命令,查了一下资料没有找到好的方法,所以干脆就直接直接调用ifconfig。def get_network_info(sys1,infolist):if sys1 = L

    17、inux:tmplist=list()ethlist = get_info( ifconfig -s|grep -v Iface|grep -v lo|awk print $1).split(n)ethInfsys = get_info( lspci | grep Ethernet).split(n)i = 0for ethi in ethlist:if ethi != :tmpdict = dict()tmpdict Description = ethInfsysi.split(:)2tmpdict IPAddress = get_info(ifconfig %s | awk /inet a

    18、ddr:/ print $2 %(ethi).split( :)1 tmpdict IPSubnet = get_info(ifconfig %s | awk /Mask/print $4%(ethi).split( :)1 tmpdict MAC = get_info(ifconfig %s | awk /HWaddr/ print $5 %(ethi)tmplist.append(tmpdict)i = i + 1for i in tmplist:print iDescriptionprint t + MAC : + t + iMAC print t + IPAddress : + t +

    19、 iIPAddress print t + IPSubnet : + t + iIPSubnet infolist.append(tmplist) 4.3 存储信息其实我首先想到的是fdisk这个命令,但是它有一个权限:无法支持超过2TB的硬盘分区,这个在当今情况下几乎是不可能。那么就用第二种分区命令了parted,由于我只想知道分区和大小,所以就在命令后面加了个过滤。如下后面再通过hdparm获取挂载盘的一些其他信息,代码如下:def get_disk_info(sys,infolist):if sys = Linux:tmplist=list()devlist = get_info( pa

    20、rted -l |grep Disk).split( n)for dev in devlist:if dev != :tmpdict = dict()tmpdict dev = dev.split() 1.split( :)0 tmpdict size = dev.split()2 temp = get_info( hdparm -I %s |awk /Model Number:/%(tmpdictdev)if temp != :tmpdict disk = temp.split(:)1else:tmpdict disk = tmplist.append(tmpdict)print tmpdi

    21、ctdev +:t+tmpdictdisk + :t + tmpdictsize infolist.append(tmplist)(5)数据库信息原理其实很简单,每个数据库启动后都会有后台进程,只要能获取到进程,就能查到它的启动参数信息,然后根据参数信息就能查到数据库的安装位置,然后通过数据库的bin目录中的程序获取该数据库的一些配置信息,比如版本号等,所以这个方法有时候不准。数据库支持系统获取方式Mysql windows通过查找mysqld进程,获取安装路径和数据库的配置信息Mysqllinux通过ps -ef |grep mysqld |grep basedir来定位进程oraclewi

    22、ndows通过查找oracle进程,获取安装路径,然后在运行sqlplus就能获取版本信息了oraclelinux通过ps -ef |grep oracle |awk /LISTENER/print $8来定位置进程,在通过sqlplus获取版本信息sqlserverwindows通过查找sqlserver进程,获取相关信息syabsewindwos通过sqlsrc进程,获取相关信息(6)已经测试环境1.win7 mysql5.1 正常 (mysql是通过mysqld程序获取信息的)2.readhat5.6 mysql5.0.77 正常3.windwos server 2008 oracle11.2.0.1.0 正常 (在windows系统下是通过查询oracl.exe程序来获取信息的)4.centos6.4 oracle11.2.0.1.0 正常 (在linux系统下是通过查询oracle的监听进程来做获取信息的 )5.windows server 2003 sqlserver2005 正常(通过检测sqlservr.exe程序来获取安装路径)6.windows server 2003 sybase ase1254 正常(通过检测sqlsrvr.exe程序来获取安装路径)7.SunOS sunv440 不支持


    注意事项

    本文(机器信息采集脚本python.docx)为本站会员主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开