shell编写自动化安装dhcp服务,shell搭建CentOS_7基础情状必发88官网

#!/bin/bash
#Auth:Darius
#自动化安装dhcp服务
#”$1″为测试IP,用来查看IP段是否能通
eno=`ifconfig|awk
‘{print $1}’|head -1|awk -F “:” ‘{print $1}’`
file=/etc/sysconfig/network-scripts/ifcfg-$eno
file1=/etc/dhcp/dhcpd.conf
dir=/etc/yum.repos.d
#ip地址设置
ping -c 4 -i 0.1 -W 2
$1 &>/dev/null
if [ $? -ne 0
];then
echo -e
“DEVICE=$eno\nTYPE=Ethernet\nONBOOT=yes\nBOOTPROTO=static”
>$file
read -p
“请输入ip地址”: ip
read -p
“请输入子网掩码”: nt
read -p “请输入网关”:
gw
echo “IPADDR=$ip”
>> $file
echo “NETMASK=$nt”
>>$file
echo “GATEWAY=$gw”
>>$file
if [ $? -eq 0
]
then
[ $? -eq 0 ] && echo
“IP地址配置成功”
else
echo
“IP地址配置失败”
fi
else
echo
“网络测试成功”
fi
#构建yum本地仓库
mkdir
/media/cdrom
mount /dev/sr0
/media/cdrom/
#设置光盘自动挂载
echo “/dev/sr0
/media/cdrom iso9660 defaults 0 0” >> /etc/fstab
mkdir $dir/bak
mv $dir/C*
$dir/bak/
echo -e
“[yum]\nname=yum\nbaseurl=file:///media/cdrom\ngpgcheck=0\nenabled=1”
>$dir/yum.repo
yum clean all
&>/dev/null
yum makecache
&>/dev/null
echo
“yum仓库构建完成”
#检测并安装dhcp服务
yum install -y dhcp
&>/dev/null
echo
“dhcp服务安装成功”
cat
/usr/share/doc/dhcp*/dhcpd.conf.sample >$file1
[ $? -eq 0 ] && echo
“模板复制成功”
cp $file1
/etc/dhcp/dhcpd.conf.bak
echo -e “subnet
192.168.1.0 netmask 255.255.255.0{\nrange 192.168.1.2
192.168.1.100;\noption routers 192.168.1.1;\n}”
>>$file1
echo
“文件配置完成”
service dhcpd start
&>/dev/null
chkconfig dhcpd on
&>/dev/null
[ $? -eq 0 ] && echo
“dhcp服务已启动”

#!/bin/bash
#Auth:Darius
#CentOS_7配置实验环境
eno=`ifconfig|awk
‘{print $1}’|head -1|awk -F “:” ‘{print $1}’`
file=/etc/sysconfig/network-scripts/ifcfg-$eno
dir=/etc/yum.repos.d
#关闭防火墙
systemctl stop
firewalld.service
systemctl disable
firewalld.service
#查看防火墙是否开启
firewall-cmd
–state
#清空防火墙规则
iptables -F
#临时关闭selinux
setenforce 0

RHEL6 无人值守搭建脚本:

#永久关闭selinux
sed -i
‘s/=enforcing/=disabled/’ /etc/sysconfig/selinux
sed -i
‘s/^SELINUX=.*/SELINUX=disabled/g’ /etc/selinux/config
#修改主机名
echo
“当前主机名:$(hostname)”
read -p
“请输入新主机名:” hostname_input
hostname
$hostname_input &> /dev/null
hostnamectl
set-hostname $hostname_input &> /dev/null

  1. #!/bin/bash   
  2. # by kumu 2012-07-28   
  3. # RHEL6 unattended deployment,用于RHEL6无人值守搭建.v0.1 
      
  4. # 有不足的地方还请见谅   
  5.   
  6. #搭建yum   
  7. Yum_build()  
  8. {  
  9.     yum clean all &>/dev/null  
  10.     num_list=$(yum list | wc -l)  
  11.     yum clean all &>/dev/null  
  12.   
  13.     if [[ $num_list < 2000 ]];then  
  14.   
  15.     mount /dev/cdrom /mnt  
  16.         if [ -d /etc/yum.repos.d/bak ];then  
  17.             mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak  
  18.         else  
  19.             mkdir /etc/yum.repos.d/bak  
  20.             mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak  
  21.         fi    
  22.   
  23. cat << EOF > /etc/yum.repos.d/base.repo  
  24. [base]  
  25. name=base  
  26. baseurl=file:///mnt  
  27. enabled=1  
  28. gpgcheck=0  
  29. EOF  
  30.     else  
  31.         echo “yum is ok!”  
  32.     fi  
  33. }  
  34.   
  35. Check_rpm()  
  36. {  
  37.     Packages=(dhcp tftp-server vsftpd syslinux)  
  38.     for i in ${Packages[@]}  
  39.     do  
  40.         if ! rpm -q $i &> /dev/null;then  
  41.              yum install $i -y &>/dev/null  
  42.         fi  
  43.     done  
  44. }  
  45.   
  46. #dhcp服务搭建以及网络安装引导文件   
  47. dhcp_build()  
  48. {  
  49.     read -p “输入所在subnet、netmask、broadcast、gateway: ” subnet netmask broadcast gateway   
  50.     read -p “输入next-server地址: ” next_server   
  51.     read -p “提供IP的范围start_ip end_ip: ” start_ip end_ip  
  52.   
  53. cat << EOF >/etc/dhcp/dhcpd.conf  
  54. subnet $subnet netmask $netmask {  
  55.   range $start_ip $end_ip;  
  56.   option routers $gateway;  
  57.   option broadcast-address $broadcast;  
  58.   default-lease-time 600;  
  59.   max-lease-time 7200;  
  60.   filename “pxelinux.0”;  
  61.   next-server $next_server;  
  62. }  
  63. EOF  
  64.   
  65. cat << EOF >/var/lib/tftpboot/pxelinux.cfg/default  
  66. default vesamenu.c32  
  67. #prompt 1   
  68. timeout 600  
  69.   
  70. display boot.msg  
  71.   
  72. menu background splash.jpg  
  73. menu title Welcome to Red Hat Enterprise Linux 6.2!  
  74. menu color border 0 #ffffffff #00000000   
  75. menu color sel 7 #ffffffff #ff000000   
  76. menu color title 0 #ffffffff #00000000   
  77. menu color tabmsg 0 #ffffffff #00000000   
  78. menu color unsel 0 #ffffffff #00000000   
  79. menu color hotsel 0 #ff000000 #ffffffff   
  80. menu color hotkey 7 #ffffffff #ff000000   
  81. menu color scrollbar 0 #ffffffff #00000000   
  82.   
  83. label linux  
  84.   menu label ^Install or upgrade an existing system  
  85.   menu default  
  86.   kernel vmlinuz  
  87.   append ks=ftp://$next_server/ks.cfg initrd=initrd.img  
  88. EOF  
  89. }  
  90.   
  91. #无人值守配置文件   
  92. ks_cfg()  
  93. {  
  94. read -p “请输入无人值守ftp地址: ” ftp  
  95. cat <<EOF >/var/ftp/ks.cfg  
  96. firewall –disabled  
  97. install  
  98. url –url=”ftp://$ftp/iso”  
  99. rootpw –iscrypted $1$A9aGh0yl$ahDdo0eAvQFAplXTeBkX//  
  100. auth  –useshadow  –passalgo=sha512  
  101. text  
  102. firstboot –disable  
  103. keyboard us  
  104. lang zh_CN  
  105. selinux –disabled  
  106. logging –level=info  
  107. reboot  
  108. timezone  Asia/Shanghai  
  109. network –bootproto=dhcp –device=eth0 –onboot=on  
  110. bootloader –location=mbr  
  111. zerombr  
  112. clearpart –all –initlabel   
  113.   
  114. part /boot –asprimary –fstype=”ext4″ –size=200  
  115. part swap –asprimary –fstype=”swap” –size=2048  
  116. part / –asprimary –fstype=”ext4″ –size=100000  
  117.   
  118. %packages  
  119. @additional-devel  
  120. @backup-client  
  121. @base 
  122. @basic-desktop  
  123. @chinese-support  
  124. @client-mgmt-tools  
  125. @compat-libraries  
  126. @console-internet  
  127. @debugging 
  128. @desktop-debugging  
  129. @desktop-platform  
  130. @desktop-platform-devel  
  131. @development 
  132. @dial-up  
  133. @directory-client  
  134. @eclipse 
  135. @emacs 
  136. @fonts 
  137. @general-desktop  
  138. @graphical-admin-tools  
  139. @graphics 
  140. @hardware-monitoring  
  141. @infiniband 
  142. @input-methods  
  143. @internet-browser  
  144. @java-platform  
  145. @kde-desktop  
  146. @large-systems  
  147. @legacy-unix  
  148. @legacy-x  
  149. @mainframe-access  
  150. @network-file-system-client  
  151. @network-tools  
  152. @performance 
  153. @perl-runtime  
  154. @remote-desktop-clients  
  155. @scientific 
  156. @security-tools  
  157. @server-platform-devel  
  158. @smart-card  
  159. @storage-client-fcoe  
  160. @storage-client-iscsi  
  161. @storage-client-multipath  
  162. @system-management  
  163. @system-management-messaging-client
     
  164. @system-management-messaging-server
     
  165. @system-management-snmp  
  166. @system-management-wbem  
  167. @technical-writing  
  168. @tex 
  169. @x11  
  170. hmaccalc  
  171. %end  
  172. EOF  
  173. }  
  174.   
  175. #调用函数   
  176. Yum_build  
  177. Check_rpm  
  178.   
  179. #拷贝系统引导文件   
  180. cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/  
  181. cp  -r /mnt/isolinux/* /var/lib/tftpboot/  
  182. mkdir /var/lib/tftpboot/pxelinux.cfg &>/dev/null  
  183. mv /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default  
  184.   
  185. #调用函数   
  186. dhcp_build  
  187. ks_cfg  
  188.   
  189. mkdir /var/ftp/iso &>/dev/null  
  190. mount /dev/cdrom /var/ftp/iso &>/dev/null  
  191.   
  192. #设置开机启动   
  193. chkconfig tftp on   
  194. chkconfig dhcpd on  
  195. chkconfig vsftpd on  
  196.   
  197. #刷新服务   
  198. service xinetd restart &>/dev/null  
  199. service dhcpd restart &>/dev/null  
  200. service vsftpd restart &>/dev/null  

#ip地址设置
rm -rf
/etc/udev/*
echo -e
“DEVICE=$eno\nTYPE=Ethernet\nONBOOT=yes\nBOOTPROTO=static”
>$file
read -p “请输入ip地址”:
ip
read -p
“请输入子网掩码”: nt
read -p “请输入网关”:
gw
echo “IPADDR=$ip”
>> $file
echo “NETMASK=$nt”
>>$file
echo “GATEWAY=$gw”
>>$file
[ $? -eq 0 ] && echo
“IP地址配置成功”
#构建yum本地仓库
mkdir
/media/cdrom
mount /dev/sr0
/media/cdrom/
#设置光盘自动挂载
echo “/dev/sr0
/media/cdrom iso9660 defaults 0 0” >> /etc/fstab
mkdir $dir/bak
mv $dir/C*
$dir/bak/
echo -e
“[yum]\nname=yum\nbaseurl=file:///media/cdrom\ngpgcheck=0\nenabled=1”
>$dir/yum.repo
yum clean all
&>/dev/null
yum makecache
&>/dev/null
echo
“yum仓库构建完成”
yum install -y vim gcc
gcc-c++ make &>/dev/null

必发88官网 1