目的:快速查询所有服务器的内存、CPU、硬盘等信息。
Server:
- #/usr/bin/python
- #2011/10/23
- import SocketServer,os,socket,sys,readline
- from SocketServer import StreamRequestHandler
- class MyHandler(StreamRequestHandler):
- def handle(self):
- ADDR = self.client_address[0]
- while 1:
- data_received=self.request.recv(1024)
- if not data_received:continue
- print 'Command:', data_received
- log='/tmp/command12_log'
- Reset='echo "################################################" > %s ' % log
- os.system(Reset)
- excute_command='%s >> %s 2>&1' % (data_received,log)
- os.system(excute_command)
- os.system('cat /tmp/command12_log')
- send_log=file(log,'rb')
- filedata=send_log.read(65535)
- send_log.close()
- self.request.send(filedata)
- if __name__=="__main__":
- HOST,PORT="192.168.1.100",9200
- my_server = SocketServer.ThreadingTCPServer((HOST,PORT),MyHandler)
- my_server.serve_forever()
- my_server.shutdown()
Client:
- #/usr/bin/python
- #2011/10/23
- import socket,readline
- dis_command=("top","sh","ksh","shutdown","reboot","init 0")
- while True :
- input= raw_input('please input your command will be executed on servers:')
- if not input:continue
- InvalidCommand=True
- for i in dis_command:
- if input == i:
- print "Invalid Command,please input other command."
- InvalidCommand=False
- break
- if InvalidCommand != True:continue
- if input == 'exit':
- break
- IP=file('ip.list')
- while True:
- line=IP.readline()
- if len(line)==0:break
- HOST=line
- PORT=9200
- s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- s.connect((HOST,PORT))
- s.send(input)
- print line
- print s.recv(65535)
- s.close()