当前位置:首页 > linux教程 > 列表

python下实现c/s模式远程操作

发布:smiling 来源: PHP粉丝网  添加日期:2015-05-05 14:12:15 浏览: 评论:0 

c/s模式远程有点像服务器与客户端一样的,我们下面利用python来做一个简单的例子,有兴趣的和小编来学学.

实现目标:通过控制端,可以实现N台主机执行同一操作.

具体代码如下:

1、控制端代码:

  1. [root@361way tmp]# vim client.py 
  2.  
  3. #!/usr/bin/env python 
  4.  
  5. import socket,os,sys 
  6.  
  7.   ost=sys.argv[1] 
  8.  
  9. ?ort=4567 
  10.  
  11. for host in os.popen('cat ip.list').readlines(): 
  12.  
  13.   print host 
  14.  
  15.   s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
  16.  
  17.   s.connect((host,port)) 
  18.  
  19.   while 1: 
  20.  
  21.     data=sys.argv[2] 
  22.  
  23.     if not data: 
  24.  
  25.         break 
  26.  
  27.     s.sendall(data) 
  28.  
  29.     data=s.recv(1024) 
  30.  
  31.     if not data: 
  32.  
  33.         break 
  34.  
  35.     print data 
  36.  
  37.   s.close() 

2、被控制端代码

  1. [root@localhost tmp]# vim server.py 
  2.  
  3. #!/usr/bin/env python 
  4.  
  5. import socket,os 
  6.  
  7. host='' 
  8.  
  9. port=4567 
  10.  
  11. s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
  12.  
  13. s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) 
  14.  
  15. s.bind((host,port)) 
  16.  
  17. s.listen(1) 
  18.  
  19. print "Server is running on port %d; press ctrl-c to terminate." % port 
  20.  
  21. while 1: 
  22.  
  23.     clientsock,clientaddr=s.accept() 
  24.  
  25.     print "connect from %s" % str(clientaddr) 
  26.  
  27.     clientfile=clientsock.makefile('rw',0) 
  28.     //phpfensi.com 
  29.     data=clientsock.recv(1024) 
  30.  
  31.     command=os.popen(data).read() 
  32.  
  33.     clientfile.write("%s" % command) 
  34.  
  35.     clientfile.close() 
  36.  
  37.     clientsock.close() 

3、操作方法

ip.list里写server端的IP地址,每行一个,如:

1.1.1.1

2.2.2.2

使用方法,将server.py部署于目标主机上,执行server.py,客户端执行.python client.py ip.list "command"即可,示例如下:

  1. [root@361way tmp]# python client.py ip.list ifconfig 
  2. 192.168.0.106 
  3. eth0      Link encap:Ethernet  HWaddr 52:54:00:D8:F3:E7 
  4.           inet addr:192.168.0.106  Bcast:192.168.0.255  Mask:255.255.255.0 
  5.           inet6 addr: fe80::5054:ff:fed8:f3e7/64 Scope:Link 
  6.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  7.           RX packets:627 errors:0 dropped:0 overruns:0 frame:0 
  8.           TX packets:262 errors:0 dropped:0 overruns:0 carrier:0 
  9.           collisions:0 txqueuelen:1000 
  10.           RX bytes:71041 (69.3 Kb)  TX bytes:55281 (53.9 Kb) 
  11. 192.168.0.110 
  12. eth0      Link encap:Ethernet  HWaddr 52:54:00:D7:F8:E7 
  13.           inet addr:192.168.0.110  Bcast:192.168.0.255  Mask:255.255.255.0 
  14.           inet6 addr: fe80::5054:ff:fed7:f8e7/64 Scope:Link 
  15.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  16.           RX packets:770 errors:0 dropped:0 overruns:0 frame:0 
  17.           TX packets:438 errors:0 dropped:0 overruns:0 carrier:0 
  18.           collisions:0 txqueuelen:1000  //phpfensi.com 
  19.           RX bytes:87223 (85.1 KiB)  TX bytes:69993 (68.3 KiB

Tags: python c s模式远程

分享到: