wKiom1O_x5rwHi8lAAEp8cXidj0344.jpg

if chkconfig --list |grep -s dhcpd&>/dev/null
then
        if ! service dhcpd status &>/dev/null
        then
        echo "警告:dhcpd服务没有启动,准备启动"
        service dhcpd start
        else
        echo "监听地址:$(netstat -tuanp|grep dhcp|awk '{print $4}')"
        echo "进程PID:$(netstat -tuanp|grep dhcp|awk '{print $6}'|awk -F/ '{print $1}')"
        fi
else
        echo "dhcpd服务不存在"
fi

注意

  1. 当卸载dhcpd服务重新安装后,执行脚本,发现dhcpd服务启动失败,是因为没有配置dhcpd服务,重新vim配置文件后,即可执行脚本

  2. 不要用rpm -qf `which dhcpd`判断dhcpd服务有没有安装,这种方法只是针对用rpm安装方式安装成功的,如果是用yum的方式安装(yum install dhcp),则用yum list installed |grep dhcpd方式查询有没有安装

    yum方式比较特殊:

    安装dhcp服务的情况

    $ yum list installed |grep dhcp
    dhcp.x86_64                              12:4.1.1-34.P1.el6                    @rhel-source
    dhcp-common.x86_64                       12:4.1.1-34.P1.el6                    @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4

    未安装dhcp服务的情况
    $ yum list installed |grep dhcp
    dhcp-common.x86_64                       12:4.1.1-34.P1.el6                    @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4

  3. 要用chkconfig --list |grep -s dhcpd方式查看dhcpd服务有没有安装