目的:快速查询所有服务器的内存、CPU、硬盘等信息。

Server:

  1. #/usr/bin/python 
  2. #2011/10/23 
  3. import SocketServer,os,socket,sys,readline 
  4.  
  5. from SocketServer import StreamRequestHandler 
  6. class MyHandler(StreamRequestHandler): 
  7.     def handle(self): 
  8.             ADDR = self.client_address[0
  9.  
  10.                 while 1
  11.                         data_received=self.request.recv(1024
  12.                         if not data_received:continue 
  13.                         print 'Command:', data_received 
  14.  
  15.                         log='/tmp/command12_log' 
  16.                         Reset='echo "################################################" > %s ' % log 
  17.                         os.system(Reset) 
  18.                         excute_command='%s >> %s 2>&1' % (data_received,log) 
  19.                         os.system(excute_command) 
  20.                         os.system('cat /tmp/command12_log'
  21.  
  22.                         send_log=file(log,'rb'
  23.                         filedata=send_log.read(65535
  24.                         send_log.close() 
  25.                         self.request.send(filedata) 
  26.  
  27.  
  28. if __name__=="__main__"
  29.         HOST,PORT="192.168.1.100",9200 
  30.         my_server = SocketServer.ThreadingTCPServer((HOST,PORT),MyHandler) 
  31.         my_server.serve_forever() 
  32.         my_server.shutdown() 

Client:

  1. #/usr/bin/python 
  2. #2011/10/23 
  3. import socket,readline 
  4.  
  5. dis_command=("top","sh","ksh","shutdown","reboot","init 0"
  6. while True : 
  7.         input= raw_input('please input your command will be executed on servers:'
  8.         if not input:continue 
  9.         InvalidCommand=True 
  10.         for i in dis_command: 
  11.                 if input == i: 
  12.                         print "Invalid Command,please input other command." 
  13.                         InvalidCommand=False 
  14.                         break 
  15.         if InvalidCommand != True:continue 
  16.         if input == 'exit'
  17.                 break 
  18.  
  19.         IP=file('ip.list'
  20.         while True
  21.                         line=IP.readline() 
  22.                         if len(line)==0:break 
  23.                         HOST=line 
  24.                         PORT=9200 
  25.                         s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
  26.                         s.connect((HOST,PORT)) 
  27.                         s.send(input) 
  28.                         print line 
  29.                         print s.recv(65535
  30.  
  31.         s.close()