|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
小知识:在过去的四年中,我经常使用CentOS,主要是作为企业Linux商业版本产品的一种替代,很多个人、企业或机构都希望拥有主流企业Linux的稳定性、可靠性和声誉。Python用WMI模块获得windowns体系的硬件信息:硬盘分区、应用情形,内存巨细,CPU型号,以后运转的过程,自启动法式及地位,体系的版本等信息。- #!/usr/bin/envpython#-*-coding:utf-8-*-importwmiimportosimportsysimportplatformimporttimedefsys_version():c=wmi.WMI()#获得操作体系版本forsysinc.Win32_OperatingSystem():print"Version:%s"%sys.Caption.encode("UTF8"),"Vernum:%s"%sys.BuildNumberprintsys.OSArchitecture.encode("UTF8")#体系是32位照样64位的printsys.NumberOfProcesses#以后体系运转的过程总数defcpu_mem():c=wmi.WMI()#CPU类型和内存forprocessorinc.Win32_Processor():#print"ProcessorID:%s"%processor.DeviceIDprint"ProcessName:%s"%processor.Name.strip()forMemoryinc.Win32_PhysicalMemory():print"MemoryCapacity:%.fMB"%(int(Memory.Capacity)/1048576)defcpu_use():#5s取一次CPU的应用率c=wmi.WMI()whileTrue:forcpuinc.Win32_Processor():timestamp=time.strftime(%a,%d%b%Y%H:%M:%S,time.localtime())print%s|Utilization:%s:%d%%%(timestamp,cpu.DeviceID,cpu.LoadPercentage)time.sleep(5)defdisk():c=wmi.WMI()#获得硬盘分区forphysical_diskinc.Win32_DiskDrive():forpartitioninphysical_disk.associators("Win32_DiskDriveToDiskPartition"):forlogical_diskinpartition.associators("Win32_LogicalDiskToPartition"):printphysical_disk.Caption.encode("UTF8"),partition.Caption.encode("UTF8"),logical_disk.Caption#获得硬盘应用百分情形fordiskinc.Win32_LogicalDisk(DriveType=3):printdisk.Caption,"%0.2f%%free"%(100.0*long(disk.FreeSpace)/long(disk.Size))defnetwork():c=wmi.WMI()#获得MAC和IP地址forinterfaceinc.Win32_NetworkAdapterConfiguration(IPEnabled=1):print"MAC:%s"%interface.MACAddressforip_addressininterface.IPAddress:print"ip_add:%s"%ip_addressprint#获得自启动法式的地位forsinc.Win32_StartupCommand():print"[%s]%s<%s>"%(s.Location.encode("UTF8"),s.Caption.encode("UTF8"),s.Command.encode("UTF8"))#获得以后运转的过程forprocessinc.Win32_Process():printprocess.ProcessId,process.Namedefmain():sys_version()#cpu_mem()#disk()#network()#cpu_use()if__name__==__main__:main()printplatform.system()printplatform.release()printplatform.version()printplatform.platform()printplatform.machine()
复制代码 小知识:CentOS是基于RedHatEnterpriseLinux(收费昂贵但口碑极佳)的源代码再编译出来的免费版,因此不仅继承RHEL优越的稳定性,还提供免费更新, |
|