Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46308
  • 博文数量: 37
  • 博客积分: 2421
  • 博客等级: 大尉
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-09 16:04
文章分类

全部博文(37)

文章存档

2010年(37)

分类: Python/Ruby

2010-07-05 14:38:34

最近初始化服务器时正好需要批量修改一组甚至几组服务器的主机名,写一个简单的脚本试试
前提当然是这些服务器都做了统一的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("\n","")
  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

..........


阅读(589) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~