#!/bin/bash
#
# Install Cobbler(Kickstart) Tools for CentOS-/
# Created by OceanHo(gzhehai@foxmail.com) AT -- #
# 操作系统发型主版本
os_release_ver=`sed -r 's#[a-zA-Z ]+(5|6|7)\..*#\1#g' /etc/redhat-release` #
# cobbler settings 文件中的server参数
# auto:表示自动 从 net_bind_device 指定的设备中获取
# 也可以手动指定
cobbler_server_ip="auto"
#
# cobbler settings 文件中的next-server参数
# auto:表示自动 从 net_bind_device 指定的设备中获取
# 也可以手动指定
cobbler_next_server_ip="auto" #
# cobbler 安装系统可以使用的默认密码(盐 / 密码:)
password_salt="`openssl rand -base64 32`"
default_password_crypted=`openssl passwd - -salt "${password_salt}" ''` # #
# 定义一个变量,用来表示cobbler涉及到网络参数自动获取网卡设备名称
# 比如:dhcpd,cobbler的settings文件中涉及到的IP地址等等
#
net_auto_dev="eth1"
dhcpd_template_geturl="https://raw.githubusercontent.com/oceanho/my-shell/master/scripts/cobbler/dhcpd_172-16-1-0_24.template" dhcpd_bind_net_prefix="172.16.1"
dhcpd_bind_net_subnet="172.16.1.0"
dhcpd_bind_net_netmask="255.255.255.0" # 显示帮助
function help()
{
echo -e `
clear
cat <<EOF
\n
\[36m
\n
功能\n
实现Cobbler程序一键安装和部署
\n
\n 参数\n
cobbler-server-ip,指定cobbler settings的server-ip参数,默认从net-auto-dev网卡设备中自动获取\n
cobbler-next-server-ip,指定cobbler settings的next-server-ip参数,默认从net-auto-dev网卡设备中自动获取\n
net-auto-dev,指定自动获取cobbler网络参数相关的配置参数网卡设备,默认eth1
\n
\n 举例\n
、使用ens34网卡设备安装并绑定cobbler的服务(dhcp/tftp)\n
----------------------------------------------------\n
sh $# cobbler-server-ip=auto cobbler-next-server-ip=auto net-auto-dev=ens34
\n
\n
、手动指定cobbler-server-ip 与 next-server-ip,dhcp.template 的子网,掩码等信息自动从eth1网卡上获取 \n
--------------------------------------------------------------------------------------------------------
\n
sh $# cobbler-server-ip=172.16.1.200 cobbler-next-server-ip=172.16.1.200 net-auto-dev=ens34 \n
\[0m
EOF
`
} case "$1" in
-h|--help|help|-help )
help
exit
;;
esac #
# 处理参数
# 参数格式要求
# key=value,比如 net-auto-dev=eth1
until [ $# -eq ]
do
if egrep -q "[a-z-]+=.*"<<<"$1"
then
k=$(sed -r 's#([a-z-]+)=.*#\1#g' <<<"$1")
v=$(sed -r 's#[a-z-]+=(.*)#\1#g' <<<"$1")
case "$k" in
"cobbler-server-ip" )
cobbler_server_ip=$v
;;
"cobbler-next-server-ip" )
cobbler_next_server_ip=$v
;;
"net-auto-dev" )
net_auto_dev=$v
;;
esac
fi
shift
done #
# 根据传入的参数,初始化一些必须的参数,比如绑定dhcpd服务的网段地址,cobbler settings配置文件中涉及到的server的ip地址等参数的初始化
function init_param()
{
str=`ip addr show $net_auto_dev | awk -F"[ ]+" 'NR==3{print $3}'`
if [ $? -ne ]
then
echo -e "\033[31m 获取网卡配置信息失败. \033[0m"
return
fi str_ip="${str%/*}"
str_netmask="${str#*/}" #
# 以下的写法只能支持 // 子网
case $str_netmask in
"" )
dhcpd_bind_net_prefix=`egrep -o "([0-9]+.){1}" <<<$str_ip`
dhcpd_bind_net_subnet="${dhcpd_bind_net_prefix}0.0.0"
dhcpd_bind_net_netmask="255.0.0.0"
;;
"" )
dhcpd_bind_net_prefix=`egrep -o "([0-9]+.){2}" <<<$str_ip`
dhcpd_bind_net_subnet="${dhcpd_bind_net_prefix}0.0"
dhcpd_bind_net_netmask="255.255.0.0"
;;
"" )
dhcpd_bind_net_prefix=`egrep -o "([0-9]+.){3}" <<<$str_ip`
dhcpd_bind_net_subnet="${dhcpd_bind_net_prefix}0"
dhcpd_bind_net_netmask="255.255.255.0"
;;
esac if [ "$cobbler_server_ip" == "auto" ]
then
cobbler_server_ip=$str_ip
fi
if [ "$cobbler_next_server_ip" == "auto" ]
then
cobbler_next_server_ip=$str_ip
fi
} #
# YUM of epel
# 自动现在cobbler先决条件项目,比如epel源等、
# 说明:如果已经有了epel源,该函数不执行安装epel源的操作,如果没有epel源,自动安装阿里云的epel源
function install_pre_require()
{
echo -e "\033[36m Installing requires \033[0m"
if [ `yum --disablerepo=\* --enablerepo=epel repolist | grep -c epel` -eq ]
then
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-${os_release_ver}.repo
fi
echo -e "\033[32m requires Completed. \033[0m"
} function install_cobbler_softs()
{
echo -e "\033[36m Installing cobbler-softs \033[0m"
fix_missing_packages_for_centos7_epel
yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd
if [ $? -ne ]
then
echo -e "\033[31m Action failed. \033[0m"
return
fi
echo -e "\033[32m cobbler-softs Completed. \033[0m"
} function fix_missing_packages_for_centos7_epel()
{
if [ $os_release_ver -eq ]
then
# rpm -ql Django14 | wc -l || e
echo -e "\033[33m May be miss Django14-1.4.21-1.el7.noarch. need you fixed. \033[0m"
# yum -y localinstall
fi
} #
# 配置DHCP服务
# 该函数会从远程主机下载,dhcpd.template 文件
# 说明:DHCP IP地址段是 -
function configure_dhcp_template()
{
echo -e "\033[36m Configure dhcp.template \033[0m"
cp /etc/cobbler/dhcp.template{,$(date +%s).ori}
wget -O /etc/cobbler/dhcp.template ${dhcpd_template_geturl}
sed -i "21s#.*#subnet $dhcpd_bind_net_subnet netmask $dhcpd_bind_net_netmask {#" /etc/cobbler/dhcp.template
sed -i "22s#.*# option subnet-mask ${dhcpd_bind_net_netmask};#" /etc/cobbler/dhcp.template
sed -i "23s#.*# range dynamic-bootp ${dhcpd_bind_net_prefix}100 ${dhcpd_bind_net_prefix}254;#" /etc/cobbler/dhcp.template
} function configure_tftpd()
{
echo -e "\033[36m Configure tftpd \033[0m"
sed -i 's#yes#no#' /etc/xinetd.d/tftp
} function configure_httpd()
{
echo -e "\033[36m Configure httpd \033[0m"
} function sync_configure()
{
echo -e "\033[36m Sync cobbler settings \033[0m"
cobbler sync
} function restart_services()
{
echo -e "\033[36m Restart cobbler's services \033[0m"
if [ $os_release_ver -ne ]
then
/etc/init.d/cobblerd restart
/etc/init.d/httpd restart
/etc/init.d/xinetd restart
else
systemctl restart cobblerd.service
systemctl restart httpd.service
systemctl restart cobblerd.service
systemctl restart xinetd.service
fi
} function configure_cobbler_services()
{
configure_dhcp_template
configure_tftpd
configure_httpd cp /etc/cobbler/settings{,$(date +%s).ori} # 备份 # 修改配置文件
sed -i "s/server: .*/server: ${cobbler_server_ip}/" /etc/cobbler/settings
sed -i "s/next_server: .*/next_server: ${cobbler_next_server_ip}/" /etc/cobbler/settings
sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings # 限制客户端只能安装一次系统
sed -ri "/default_password_crypted/s#(.*: ).*#\1\"`openssl passwd -1 -salt 'oceanhoasdhakdhakjs' '123456'`\"#" /etc/cobbler/settings
#sed -i 's#yes#no#' /etc/xinetd.d/rsync
sed -i 's#yes#no#' /etc/xinetd.d/tftp
} # 程序入口
function main()
{ # 初始化参数
init_param install_pre_require
install_cobbler_softs
configure_cobbler_services
sync_configure
restart_services
cobbler get-loaders
sync_configure
cobbler check
} # 执行安装程序操作
main

一键安装cobbler脚本的更多相关文章

  1. linux一键安装vncserver脚本

    title: linux一键安装vncserver脚本 date: 2016-04-11 14:32:04 tags: --- linux多数情况下是作为服务器使用的,管理员一般也喜欢使用命令行来管理 ...

  2. Rehat一键安装mysql脚本和备份数据库脚本

    Rehat一键安装mysql脚本 ##说明:适用,Rehat 5 6 7 1.运行状态,运行成功输出mysql临时密码 2.代码如下 #!/bin/bash #获取系统信息 sudo cat /etc ...

  3. (转)CentOS一键安装Nginx脚本

    原文:https://www.xiaoz.me/archives/10301 https://blog.slogra.com/post-676.html-----centos7一键安装nginx脚本

  4. rsync服务端一键安装rsync脚本(非源码)

    export RSYNC_PASSWORD=123 USER=rsync AUTHUSERS=bck MK=backupmk local_dir=/backup yum remove rsync &a ...

  5. 一键安装lnmp脚本

    前段时间一直在找一个快速部署lnmp环境的方法,也没找着,就自己写了一个,结合网上的大神们的.我的方法是脚本结合源码包,很多依赖裤都是yum安装的,这都在脚本里面,需要的源码包我都下载好了,打包成压缩 ...

  6. 【转载】一键安装maven脚本

    #!/bin/bashif [ -z "${MAVEN_HOME}" ]; then #得到时间 TIME_FLAG=`date +%Y%m%d_%H%M%S` #备份配置文件 c ...

  7. Node_exporter一键安装部署脚本(Shell)

    #!/bin/bash # # rhel7. 安装node_exporter 用于监控数据采集 # Usage: # sh addNode.sh #Logs: /var/log/messages #H ...

  8. centos下的redis一键安装shell脚本

    #!/bin/bash yum install cpp binutils glibc-kernheaders glibc-common glibc-devel gcc make wget #安装依赖库 ...

  9. rsync客户端一键安装rsync脚本(源码)

    客户端 read -np "请输入源码rsync的URL 地址 包名(以空格为分隔符,别带/):" URL DZ BM yum remove -y rsync &>& ...

随机推荐

  1. GreyMagic

    hearthbuddy中的一段代码 // Token: 0x06001A79 RID: 6777 RVA: 0x000DD024 File Offset: 0x000DB224 internal In ...

  2. 一、基础篇--1.1Java基础-hashCode和equals方法的区别和联系

     hashCode和equals方法的区别和联系  两个方法的定义 equals(Object obj)方法用来判断两个对象是否"相同",如果"相同"则返回tr ...

  3. ControlTemplate in WPF —— Button

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  4. PyCharm安装+破解

    PyCharm 是一款功能强大的 Python 编辑器,具有跨平台性,鉴于目前最新版 PyCharm 使用教程较少,为了节约时间,来介绍一下 PyCharm 在 Windows下是如何安装的. 这是 ...

  5. javascript-->getElementsByClass

        //通过类名获取元素    //obj->目标元素的上一层元素  cName->目标类名 tagName->目标的标签类型(可缺省)    function getEleme ...

  6. CTF—攻防练习之HTTP—SQl注入(get)

    攻击机:192.168.32.152 靶机 :192.168.32.157 扫描靶机扫端口: 开放了ssh和80看下ssh版本有没有漏洞,searchsplot下,没有发现 dirb扫描下目录,有个a ...

  7. python学习之模块-模块(三)

    5.6 time 模块 已经知道的常用的time方法:time.time()获取当前时间的时间戳:time.sleep(num)线程推迟指定的时间(秒)后再继续往下运行. 时间的表示方式 大致可以分为 ...

  8. LeetCode.1018-可被5整除的二进制数(Binary Prefix Divisible By 5)

    这是小川的第379次更新,第407篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第241题(顺位题号是1018).给定0和1的数组A,考虑N_i:从A[0]到A[i]的第 ...

  9. 主机加固之windows2003

    这篇与上一篇的win7主机加固内容大体类似,部分有些不同.这篇也可以用来尝试加固windows XP. 1. 配置管理 1.1用户策略 注意:在对Windows系统加固之前先新建一个临时的系统管理员账 ...

  10. 意想不到的JavaScript(每日一题3)

    题目: 答案: 1 4 3 2 解析: