Python多线程应用实例 使用多线程批量修改主机名

默北 pythonPython多线程应用实例 使用多线程批量修改主机名已关闭评论9,300字数 585阅读1分57秒阅读模式

前面说到了python实现多线程,具体参见:https://www.ttlsa.com/python/python-multi-threaded-tutorial/

下面来举个例子来说明下:文章源自运维生存时间-https://www.ttlsa.com/python/python-multithreaded-applications-batch-change-the-host-name-to-use-multiple-threads/

新上的服务器初始化时,需要批量修改服务器主机名。文章源自运维生存时间-https://www.ttlsa.com/python/python-multithreaded-applications-batch-change-the-host-name-to-use-multiple-threads/

代码如下所示:文章源自运维生存时间-https://www.ttlsa.com/python/python-multithreaded-applications-batch-change-the-host-name-to-use-multiple-threads/

#!/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()
文章源自运维生存时间-https://www.ttlsa.com/python/python-multithreaded-applications-batch-change-the-host-name-to-use-multiple-threads/文章源自运维生存时间-https://www.ttlsa.com/python/python-multithreaded-applications-batch-change-the-host-name-to-use-multiple-threads/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 31/12/2014 01:00:50
  • 转载请务必保留本文链接:https://www.ttlsa.com/python/python-multithreaded-applications-batch-change-the-host-name-to-use-multiple-threads/