标签:脚本 python 系统管理员 命令行解析器 操作系统
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
import wmi
import win32api
except ImportError:
wmi = None
import sys,platform
import subprocess
import os
def get_system_info(c,sys):
if sys
== "Windows":
for sys in c.Win32_OperatingSystem():
print "Version
:\t%s" % sys.Caption.encode( "GBK")
print "Vernum :\t%s" %
sys.BuildNumber
def 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 = int(cs[ 0].TotalPhysicalMemory)/ 1024/1024
print "TotalPhysicalMemory
:" + ‘\t‘ +
str(MemTotal) + "M"
#tmpdict ["MemFree"]
= int( sys[0].FreePhysicalMemory)/1024
SwapTotal = int(pfu[ 0].AllocatedBaseSize)
print "SwapTotal
:" + ‘\t‘ +
str(SwapTotal) + "M"
#tmpdict ["SwapFree"]
= int( pfu[0].AllocatedBaseSize - pfu[0].CurrentUsage)
def get_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)
tmpdict["size" ]=str(long(physical_disk.Size)/1024/1024/1024)+"G"
tmpdict[ "dev"]
= str(physical_disk.MediaType)
tmplist.append(tmpdict)
print tmpdict["dev" ]+":\t"+tmpdict["disk"]
+ ‘ :\t‘ + tmpdict["size" ]
infolist.append(tmplist)
def get_cpu_info(c,sys,infolist):
if sys
== "Windows":
tmplist=list()
tmpdict=dict()
tmpdict[ "CpuCores"]
= 0
for cpu in c.Win32_Processor():
tmpdict[ "model name"]
= cpu.Name
try:
tmpdict[ "CpuCores"]
= cpu.NumberOfCores
except:
tmpdict[ "CpuCores"]
+= 1
tmpdict[ "CpuClock"]
= cpu.MaxClockSpeed
print ‘CpuType
:\t‘ + str(tmpdict["model name"])
print ‘CpuCores
:\t‘ + str(tmpdict["CpuCores"])
tmplist.append(tmpdict)
#infolist.append(tmplist)
return tmplist
def get_network_info(c,sys,infolist):
if sys
== "Windows":
tmplist=list()
for interface in c.Win32_NetworkAdapterConfiguration
(IPEnabled=1):
tmpdict=dict()
tmpdict[ "Description"]
= interface.Description
tmpdict[ "IPAddress"]
= interface.IPAddress[0 ]
tmpdict[ "IPSubnet"]
= interface.IPSubnet[0 ]
tmpdict[ "MAC"]
= interface.MACAddress
tmplist.append(tmpdict)
for i in tmplist:
print
i["Description"]
print ‘\t‘ + "MAC
:" + ‘\t‘ +
i["MAC" ]
print ‘\t‘ + "IPAddress
:" + ‘\t‘ +
i["IPAddress" ]
print ‘\t‘ + "IPSubnet
:" + ‘\t‘ +
i["IPSubnet" ]
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.CommandLine)
name = process.Name
if name.find(process_name)
>= 0:
cmd = temp
return cmd
def get_info(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell= False)
return p.communicate()[ 0].split( "\n")[0 ]
if __name__ == "__main__":
sys = platform.system()
infolist = list()
c = wmi.WMI ()
|
ef get_cpu_info(sys,infolist):
cpuinfo=dict()
procinfo=dict()
tempdict = dict()
templist = list()
if sys
== "Linux":
‘‘‘ Return the information in /proc/cpuinfo
as a dictionary in the following format:
cpu_info[‘proc0‘]={...}
cpu_info[‘proc1‘]={...}
‘‘‘
nprocs = 0
f = file( "/proc/cpuinfo",‘r‘)
while True :
line = f.readline()
if len(line)
== 0:
break
if not line.strip():
# end of one processor
cpuinfo[ ‘proc %s‘ %
nprocs] = procinfo
nprocs=nprocs+ 1
# Reset
procinfo=dict()
else:
if len(line.split( ‘:‘))
== 2 :
temp1 = line.split( ‘:‘)[0 ].strip()
temp2 = line.split( ‘:‘)[1 ].strip()
tempdict[temp1] = temp2
print temp1+"
: "+temp2
else:
procinfo[line.split( ‘:‘)[0 ].strip()]
= ‘‘
templist.append(tempdict)
# infolist.append(tempdict)
return templist
|
def get_network_info(sys1,infolist):
if sys1
== ‘Linux‘:
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 = 0
for ethi in ethlist:
if ethi
!= "":
tmpdict = dict()
tmpdict[ "Description"]
= ethInfsys[i].split(":")[2]
tmpdict[ "IPAddress"]
= get_info(‘ifconfig %s | awk \‘/inet addr:/{
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 + 1
for i in tmplist:
print
i["Description"]
print ‘\t‘ + "MAC
:" + ‘\t‘ +
i["MAC" ]
print ‘\t‘ + "IPAddress
:" + ‘\t‘ +
i["IPAddress" ]
print ‘\t‘ + "IPSubnet
:" + ‘\t‘ +
i["IPSubnet" ]
infolist.append(tmplist)
|
def get_disk_info(sys,infolist):
if sys
== "Linux":
tmplist=list()
devlist = get_info( "parted -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:/\‘"%(tmpdict["dev"]))
if temp
!= "":
tmpdict[ "disk"]
= temp.split(":")[1]
else:
tmpdict[ "disk"]
= ""
tmplist.append(tmpdict)
print tmpdict["dev" ]+":\t"+tmpdict["disk"]
+ ‘ :\t‘ + tmpdict["size" ]
infolist.append(tmplist)
|
数据库 | 支持系统 | 获取方式 |
Mysql | windows | 通过查找mysqld进程,获取安装路径和数据库的配置信息 |
Mysql | linux | 通过ps -ef |grep mysqld |grep basedir来定位进程 |
oracle | windows | 通过查找oracle进程,获取安装路径,然后在运行sqlplus就能获取版本信息了 |
oracle | linux | 通过ps -ef |grep oracle |awk \‘/LISTENER/{print $8}\‘来定位置进程,在通过sqlplus获取版本信息 |
sqlserver | windows | 通过查找sqlserver进程,获取相关信息 |
syabse | windwos | 通过sqlsrc进程,获取相关信息 |
标签:脚本 python 系统管理员 命令行解析器 操作系统
原文地址:http://blog.csdn.net/caidaoqq/article/details/44706159