首页 > 代码库 > dhcp配置简单脚本

dhcp配置简单脚本

dhcp_install.sh
#!/bin/bash
#安装yum源
#配置
#启动

#mount -o loop /dev/cdrom /mnt
#echo "mount -o loop /dev/cdrom /mnt">>/etc/rc.local

cat >/etc/yum.repos.d/dvd.repo <<ENDF
[autoyum]
baseurl=file:///mnt
gpgcheck=0
ENDF
yum clean all && yum list all &>/dev/null && echo "yum is ok"

yum install -y dhcp &>/dev/null && echo "dhcp is installed"

dhcp_configure.sh
#!/bin/bash
read -p "please input your IP:" IP
read -p "please input your NETMASK:" NET
read -p "please input your GATEWAY:" GW
read -p "please input your DNS1:" DNS1
read -p "please input your DNS2:" DNS2

SAM=/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
ITR=$(ifconfig eth0 |grep -Eo ‘([0-9]{1,3}\.){2}[0-9]{1,3}‘|head -1)
MASK=$(ifconfig eth0 |grep -Eo ‘(([0-9]{1,3}\.){3}[0-9]{1,3})‘|grep ^255)
BOR_ADD=$(ifconfig eth0 |grep -Eo ‘(([0-9]{1,3}\.){3}[0-9]{1,3})‘|grep 255$)
MAC=$(ifconfig eth0 |grep -Eo ‘(([0-9 A-Z a-z]{1,2}:){5}[0-9 A-Z a-z]{1,2})‘)

export SAM ITR MASK BOR_ADD MAC

cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<ENDF
DEVICE="eth0"
BOOTPROTO="none"
HWADDR="00:0C:29:23:15:40"
ONBOOT="yes"
IPADDR=$IP
NETMASK=$NET
GATEWAY=$GW
DNS1=$DNS1
DNS2=$DNS2
ENDF

read -p "please input your HOSTNAME:" HN
cat >/etc/sysconfig/network <<ENDF
NETWORKING=yes
HOSTNAME=$HN
ENDF

echo "$IP $HN ">>/etc/hosts &>/dev/null && echo "hostname is ok"


\cp -p $SAM /etc/dhcp/dhcpd.conf &>/dev/null && echo "copy is ok"

cat >/etc/dhcp/dhcpd.conf <<ENDF
option domain-name "$HN";
option domain-name-servers $DNS1, $DNS2;  
default-lease-time 600;
max-lease-time 7200;

subnet $ITR.0 netmask $MASK {
 range $ITR.100 $ITR.200;
 option routers $GW;
 option broadcast-address $BOR_ADD;
 default-lease-time 600;
 max-lease-time 7200;
}

host fantasia {
 hardware ethernet $MAC;
 fixed-address $GW;
}
ENDF
#检测dhcpd的语法是否正确
#dhcpd -d &>/dev/null && echo "dhcpd grammer is ok"

service dhcpd restart &>/dev/null && echo "dhcpd is ok"
chkconfig dhcpd on

dhcp配置简单脚本