python配置主机名脚本

Posted effortsing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python配置主机名脚本相关的知识,希望对你有一定的参考价值。

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 ‘$ahostname=test1‘ /etc/hostname"], shell=True)
    subprocess.call(["sed -i ‘$ahostname=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配置主机名脚本的主要内容,如果未能解决你的问题,请参考以下文章