实现目标: 通过控制端,可以实现N台主机执行同一操作。 具体代码如下: 1、控制端代码 1[root@361way tmp]# vim client.py 2#!/usr/bin/env python 3import socket,os,sys 4 ost=sys.argv[1] 5▽ort=4567 6for host in os.popen('cat ip.list').readlines(): 7 print host 8 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 9 s.connect((host,port)) 10 while 1: 11 data=sys.argv[2] 12 if not data: 13 break 14 s.sendall(data) 15 data=s.recv(1024) 16 if not data: 17 break 18 print data 19 s.close() 2、被控制端代码 1[root@localhost tmp]# vim server.py 2#!/usr/bin/env python 3import socket,os 4host='' 5port=4567 6s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 7s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) 8s.bind((host,port)) 9s.listen(1) 10print "Server is running on port %d; press ctrl-c to terminate." % port 11while 1: 12 clientsock,clientaddr=s.accept() 13 print "connect……
Continue reading