获取服务器型号、内存、CPU信息
#!/usr/bin/python #2013-02-18 by larry import os import sys import re from collections import Counter def getSN(): s=os.popen('dmidecode -s system-serial-number') n=os.popen('dmidecode -s system-product-name') sn=s.readline().strip() name=n.readline().strip() s.close() if ( len(sn) > 0 ): print "Product Name:",name print "Serial Number:",sn,"\n" else: return -1 def getCPU(): opfile=file('/proc/cpuinfo','r') cpucount=0 phcpucount=0 cpumodel=[] L1=[] for line in opfile.readlines(): i=line.strip('\n') if re.match(r'^processor',i): cpucount=cpucount+1 elif re.match(r'^physical id',i): L1.append(i) elif re.findall('model name',i): cpumodel.append(i.split(':')[1]) else: pass opfile.close() for num in Counter(L1).keys(): phcpucount=phcpucount+1 print "Processor number:",cpucount print "Physical CPU number:",phcpucount print "CPU Model:",cpumodel[0].strip(),"\n" def getMem(): m=os.popen('grep MemTotal /proc/meminfo') max=os.popen('dmidecode -t memory') swm=os.popen('grep SwapTotal /proc/meminfo') L=[] for line in max.readlines(): R1=line.strip("\n") if re.findall('Maximum',R1): sw,se=R1.split(":") L.append(se) max.close() for line1 in swm.readlines(): sw1,sw2,sw3=line1.split() L.append(sw2) for line2 in m.readlines(): m1,m2,m3=line2.split() L.append(m2) print "Memory Total:",int(L[2])/1000/1000,"G" print "Swap Total:",int(L[1])/1024,"M" print "Max support Memory:",L[0] if __name__ == '__main__': print "\x1b[0;34m+++++++++++Device SN+++++++++\x1b[0m" getSN() print "\x1b[0;34m+++++++++++Cpu Info++++++++++\x1b[0m" getCPU() print "\x1b[0;34m+++++++++++Memory +++++++++++\x1b[0m" getMem()
执行脚本结果:
+++++++++++Device SN+++++++++
Product Name: PowerEdge 860
Serial Number: BBNRF2X
+++++++++++Cpu Info++++++++++
Processor number: 4
Physical CPU number: 1
CPU Model: Intel(R) Xeon(R) CPU X3210 @ 2.13GHz
+++++++++++Memory +++++++++++
Memory Total: 4 G
Swap Total: 8189 M
Max support Memory: 8 GB
pexpect使用手记
一、安装easy_install工具
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py 安装easy_install工具(这个脚本会自动去官网搜索下载并安装)
python ez_setup.py -U setuptools 升级easy_install工具
二、安装pexpect
easy_install Pexpect
测试一下:
[root@OMS python]# python
Python 2.7.3rc1 (default, Nov 7 2012, 15:03:45)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> import pxssh
>>>
ok已经安装完成。
三、写一个脚本给远程服务器发送命令,并返回结果。
脚本内容:
#!/usr/bin/python #2013-01-16 by larry import pexpect def login(port,user,passwd,ip,command): child=pexpect.spawn('ssh -p%s %s@%s "%s"' %(port,user,ip,command)) o='' try: i=child.expect(['[Pp]assword:','continue connecting (yes/no)?']) if i == 0: child.sendline(passwd) elif i == 1: child.sendline('yes') else: pass except pexpect.EOF: child.close() else: o=child.read() child.expect(pexpect.EOF) child.close() return o hosts=file('hosts.list','r') for line in hosts.readlines(): host=line.strip("\n") if host: ip,port,user,passwd,commands= host.split(":") for command in commands.split(","): print "+++++++++++++++ %s run:%s ++++++++++++" % (ip,command), print login(port,user,passwd,ip,command) hosts.close()
使用方法:python scripts.py
host.list文件内容如下:
192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami
192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami
返回结果:
+++++++++++++++ 192.168.0.21 run:cat /etc/redhat-release ++++++++++++
Red Hat Enterprise Linux Server release 4
+++++++++++++++ 192.168.0.21 run:df -Th ++++++++++++
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/cciss/c0d0p6
ext3 5.9G 4.4G 1.2G 80% /
/dev/cciss/c0d0p7
ext3 426G 362G 43G 90% /opt
/dev/cciss/c0d0p5
ext3 5.9G 540M 5.0G 10% /var
/dev/cciss/c0d0p3
ext3 5.9G 4.1G 1.5G 74% /usr
/dev/cciss/c0d0p1
ext3 487M 17M 445M 4% /boot
tmpfs tmpfs 4.0G 0 4.0G 0% /dev/shm
+++++++++++++++ 192.168.0.21 run:whoami ++++++++++++
root
python遍历目录
#!/usr/bin/python # -*- coding: UTF-8 -*- import os,sys def Vallfile(path): for dirnames,dirs,files in os.walk(path,True): for filepath in files: print os.path.join(dirnames, filepath) if __name__=="__main__": path =sys.argv[1] Vallfile(path)
使用方法:
python findallfiles.py 目录路径
例如:
[root@OMS tmp]# python bcd.py /usr/local/nagios
/usr/local/nagios/share/index.php
/usr/local/nagios/share/robots.txt
/usr/local/nagios/share/side.php
/usr/local/nagios/share/rss-newsfeed.php
/usr/local/nagios/share/config.inc.php
/usr/local/nagios/share/rss-corefeed.php
快乐程序员-第一天
I'm so hapyy to come here.
I can start my programmer's road now.