项目需求:
初始化服务器时,需要批量修改一组甚至几组服务器的主机名。
分享一个简单的python 多线程脚本,需要这些服务器都做了统一的key,你懂的。
代码:
#!/usr/bin/env python
# -*- coding : UTF-8 -*-
# Author : wanghaoyu - wanghaoyu1625@gmail.com
# QQ : 42030925
# Last modified: 2010-07-05 13:32
# edit: www.jb200.com
# Filename: host_modify.py
# Description:
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()
hostadd 文件内容: