码迷,mamicode.com
首页 > 编程语言 > 详细

python配置主机名脚本

时间:2019-01-30 17:14:08      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:脚本   linux   readlines   cat   proc   disable   sysconfig   config   profile   

1、准备hosts模板

mkdir -p /k8s/profile

cat >/k8s/profile/hosts<<EOF
192.168.0.91 test1
192.168.0.92 test2
192.168.0.93 test3
EOF


2、编写脚本

cat > hostname.py <<EOF 
#!/usr/bin/python
#-*- codinig: UTF-8 -*-
from __future__ import print_function
import os
import shutil
import tarfile
import subprocess


def environment_format():
    subprocess.call(["hostnamectl set-hostname test1"], shell=True)
    #配置hosts解析
    ms=open("/k8s/profile/hosts")
    for line in ms.readlines():
        with open(/etc/hosts,a+) as mon:
            mon.write(line)
    ms.close()
    subprocess.call(["sed -i ‘$a\hostname=test1‘ /etc/hostname"], shell=True)
    subprocess.call(["sed -i ‘$a\hostname=test1‘ /etc/sysconfig/network"], shell=True)
    subprocess.call(["sed -i ‘s/enforcing/disabled/g‘ /etc/selinux/config"], shell=True)
    subprocess.call(["sed -i ‘s/SELINUX=permissive/SELINUX=disabled/‘ /etc/sysconfig/selinux"], shell=True)
    subprocess.call(["sed -i ‘s/\/dev\/mapper\/centos-swap/#\/dev\/mapper\/centos-swap/g‘ /etc/fstab"], shell=True)
    subprocess.call(["systemctl stop firewalld && systemctl disable firewalld"], shell=True)
    subprocess.call(["reboot"], shell=True)

def func_list():
    environment_format()

def main():
    func_list()
if __name__ == __main__:
    main()
EOF

 

python配置主机名脚本

标签:脚本   linux   readlines   cat   proc   disable   sysconfig   config   profile   

原文地址:https://www.cnblogs.com/effortsing/p/10338651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!