python实现多线程批量修改主机名代码实例讲解
最近在看多线程,初始化服务器时正好需要批量修改一组甚至几组服务器的主机名,写一个简单的脚本试试,能看得到的朋友,希望能有所收获。前提当然是这些服务器都做了统一的key......
#!/usr/bin/env python # -*- coding : UTF-8 -*- # Author : wanghaoyu - wanghaoyu1625@gmail.com # QQ : 42030925 # Last modified: 2010-07-05 13:32 # 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 文件内容为:
192.168.76.132 node1.example.com 192.168.76.133 node2.example.com
|
补充:Web开发 , Python ,