- A+
所属分类:python
前面说到了python实现多线程,具体参见:http://www.ttlsa.com/python/python-multi-threaded-tutorial/
下面来举个例子来说明下:
新上的服务器初始化时,需要批量修改服务器主机名。
代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/usr/bin/env python # -*- coding : UTF-8 -*- import re import os,sys,datetime import threading class ThreadClass(threading.Thread): def run(self): now = datetime.datetime.now() print "%s over at time %s" % (self.getName(),now) f = file(hostadd,r) for line in f.readlines(): ip = re.findall(d .d .d .d ,line)[0] host = line[15:].replace(" ","") cmd = "hostname %s && sed -i s/HOSTNAME=.*/HOSTNAME=\%s/g /etc/sysconfig/network" % (host,host) process = os.popen(ssh %s % ip "%s" % cmd).read() print process t = ThreadClass() t.start() |

微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~