手动搭建DHCP基础服务的快捷脚本

1.脚本代码

代码如下(示例):dhcp.sh

#!/bin/bash

yum list dhcp
yum -y install dhcp
rpm -qc dhcp
cat>> /etc/dhcp/dhcpd.conf <<EOF
option domain-name "example.org";
option domain-name-servers 192.168.11.2, 223.5.5.5;

default-lease-time 21600;
max-lease-time 43200;

ddns-update-style none;

log-facility local7;

subnet 192.168.11.0 netmask 255.255.255.0 {
  range 192.168.11.123 192.168.11.199;
  option routers 192.168.11.2;
}
EOF
systemctl enable --now dhcpd
systemctl start dhcpd
systemctl status dhcpd

touch hosts.txt
if  ! rpm -q expect > /dev/null
then
    echo "###expect 未安装,现在安装###"
    yum install -y expect &>/dev/null     ###安装expect###
    if [ $? -ne 0 ]
    then
        echo "###expect 安装失败###"
        exit 1
    fi
fi
if [ ! -f ~/.ssh/id_rsa ]
then
    echo "###请按3次enter键###"
    ssh-keygen -t rsa
fi

passwd="123123"
ssh_expect () {
    expect -c "set timeout -1;
    spawn ssh-copy-id -f $1

    expect {
        "yes/no" { send -- yes\r;exp_continue;}
        "password:" { send -- $2\r;exp_continue;}
        eof:
    }";
}
for ip in `cat hosts.txt |awk '{print $1}'`
do

 ssh_expect $ip $passwd
 done
ssh root:123123@192.168.11.12   <<eeoff
rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0
cat >> /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE=eth0 
NAME=eth0 
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp 
#PADDR=192.168.11.12 
#ATEWAY=192.168.11.2
#ETMASK=255.255.255.0
#NS1=223.5.5.5
#NS2=192.168.11.2
EOF

systemctl restart network
ip a
eeoff
echo done!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值