[root@us-1-217 install]
import os, crypt
base_path = '/opt/opmgmt/install'
pxe_path = os.path.join(base_path, 'pxelinux.cfg')
kickstart_path = os.path.join(base_path, 'kickstart')
sitename = 'us.install.suntv.tv'
installhost = '10.150.1.217'
yumhost = 'us.yum.suntv.tv'
hosts = 'hosts.txt'
os = 'centos7'
password = 'password'
rootpw = crypt.crypt(password, '$6$MySalt')
def generate_pxe_file(os, mac, sitename, pxe_path):
pxe = '''default menu.c32
prompt 0
timeout 100
LABEL %s
MENU DEFAULT
MENU LABEL %s
KERNEL %s/vmlinuz
APPEND initrd=%s/initrd.img ks=http://%s/kickstart/%s ksdevice=link ramdisk_size=102400 console=ttyS1,115200
''' % (os, mac, os, os, sitename, mac)
filename = pxe_path + '/01-' + '-'.join(mac.split(':'))
with open(filename, 'w') as f:
f.write(pxe)
print 'generate pxe file: %s' % ('01-' + '-'.join(mac.split(':')))
def generate_kickstart_file(os, sitename, rootpw, kickstart_path, dev, prefix, mac, private_ip, private_mask, public_ip, public_mask, default_gw):
if dev == 'em':
private_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s1 << _EOF_
DEVICE=%s1
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, private_ip, private_mask)
if public_ip != '0' and public_mask !='0':
public_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s2 << _EOF_
DEVICE=%s2
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, public_ip, public_mask)
else:
public_interface = ''
if dev == 'eth':
private_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s0 << _EOF_
DEVICE=%s0
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, private_ip, private_mask)
if public_ip != '0' and public_mask !='0':
public_interface = '''cat > /etc/sysconfig/network-scripts/ifcfg-%s1 << _EOF_
DEVICE=%s1
ONBOOT=yes
BOOTPROTO=static
IPADDR=%s
NETMASK=%s
_EOF_
''' % (dev, dev, public_ip, public_mask)
else:
public_interface = ''
network = '''cat > /etc/sysconfig/network << _EOF_
NETWORKING=yes
HOSTNAME=%s-%s-%s
GATEWAY=%s
_EOF_
''' % (prefix, private_ip.split('.')[2], private_ip.split('.')[3], default_gw)
dns = '''cat > /etc/resolv.conf << _EOF_
nameserver 8.8.8.8
nameserver 8.8.4.4
_EOF_
'''
ssh = '''sed -i 's/#Port 22/Port 29922/g' /etc/ssh/sshd_config
sed -i 's/GSSAPIAuthentication yes/#GSSAPIAuthentication yes/g' /etc/ssh/sshd_config
sed -i 's/#GSSAPIAuthentication no/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
'''
tcp_ip = '''cat >> /etc/sysctl.conf << _EOF_
fs.file-max = 100000
#net.ipv4.tcp_syncookies = 1
#net.ipv4.tcp_tw_reuse = 1
#net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65000
#net.ipv4.tcp_max_syn_backlog = 8192
#net.ipv4.tcp_max_tw_buckets = 5000
_EOF_
'''
limit ='''cat >> /etc/security/limits.conf << _EOF_
* soft nofile 100000
* hard nofile 100000
_EOF_
'''
hosts ='''cat >> /etc/hosts << _EOF_
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
%s %s
_EOF_
''' % (installhost, yumhost)
yum ='''
rm -rf /etc/yum.repos.d/*
curl http://%s/centos6.repo -o /etc/yum.repos.d/centos.repo
curl http://%s/epel.repo -o /etc/yum.repos.d/epel.repo
''' % (yumhost, yumhost)
kickstart = '''text
keyboard us
timezone Asia/Shanghai
lang en_US.UTF-8
skipx
auth --enableshadow --passalgo=sha512
rootpw --iscrypted %s
#zerombr
bootloader --boot-drive=sda --location=mbr
ignoredisk --only-use=sda
clearpart --drives=sda --all
#part swap --fstype='swap' --ondisk=sda --size=8000
part biosboot --fstype='biosboot' --size=1
part / --fstype='xfs' --ondisk=sda --size=50000
part /opt --fstype='xfs' --ondisk=sda --size=1 --grow
network --bootproto=dhcp --device=%s --activate
install
url --url='http://%s/%s'
logging level=info
firewall --disabled
selinux --disabled
firstboot --disabled
services --enabled=network,rc-local --disabled=NetworkManager,postfix
reboot
%%packages
@core
%%end
%%pre
/usr/sbin/parted -s /dev/sda mklabel gpt
%%end
%%post
%s
%s
%s
%s
%s
%s
%s
%s
%s
%%end
''' % (rootpw, mac, sitename, os, private_interface, public_interface, network, dns, ssh, tcp_ip, limit, hosts, yum)
filename = kickstart_path + '/' + mac
with open(filename, 'w') as f:
f.write(kickstart)
print 'generate kickstart file: %s ' % mac
with open(hosts, 'r') as f:
for host in f:
dev, prefix, mac, private_ip, private_mask, public_ip, public_mask, default_gw = host.strip('\n').split(' ')
generate_pxe_file(os, mac.lower(), sitename, pxe_path)
generate_kickstart_file(os, sitename, rootpw, kickstart_path, dev, prefix, mac.lower(), private_ip, private_mask, public_ip, public_mask, default_gw)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类