如何在另一个文件中为MAC地址保留dhcp?


0

我有类似ip_mac的文件,其中包含学生年份及其各自的mac地址。我只想让他们拥有dhcp租约。是否有可能在ubuntu中执行此操作。

94:92:66:38:a6:79                             "Prajwal Bhandary"                       2017            cs            Mob
30:e1:71:c9:1b:43                             "Prajwal Bhandary"                       2017            cs            Lan
94:65:2d:23:a8:4d                             "Unique Karki"                           2017            cs            Mob
84:8f:69:ca:15:00                             "Anup Paudel"                            2017            arch          Mob
88:83:22:eb:1e:41                             "Anup Paudel"                            2017            arch          Lan

并且任何学生都可以添加到任何部分,因此,如果我重新启动isc dhcp服务器,则不应断开学生的连接。

一种方法是在/etc/dhcp/dchpd.conf中使用这样的格式

host student1 {
hardware ethernet 94:92:66:38:a6:79;
fixed-address 10.0.0.101;
}

但问题是我需要为每个学生设置IP。而且有400多个,所以管理IP是夜马吧?

另一种方法是默认允许,但是我们有IP限制,例如只有500 ip,所以如果不必要的人连接学生不能使用,那么这种情况有解决方案吗?

要么,

您可以只使用mac地址并允许dhcp租用,而不是编写固定地址吗?

是否可以通过扫描文件来允许dhcp租约?


通过外壳脚本运行该输入文件以生成dhcp.conf有什么问题?您不能经常更改它!
JayEye

@JayEye我们可以使用shell脚本,但是我怎么知道我应该给他们哪个IP。插入随机ip会使一切混乱,对吗?
余震

如果您手动构建了dhcpd.conf,将采用相同的方法!
JayEye

在dhcpd中的JayEye中,我们需要手动指定ip,不是吗?有没有办法我只能在dhcpd文件中没有ip指定mac。?
余震

Answers:


0

我从保留的一个旧脚本中创建了此脚本。

#!/bin/bash

if [ "$EUID" -ne 0 ];
  then echo "Please run as root (sudo)"
  exit
fi

PATH_DHCP_FILE="./test.conf"
PATH_DHCP_TRUST_FILE="./trust_host"
PATH_STUDENTS_FILE="students.list"
PATH_DHCP_LEASES="/var/lib/dhcp/dhcpd.leases"

DATE=`date '+%Y-%m-%d %H:%M'`

IP_RANGE_DEFAULT='192.168.1.50 192.168.1.100'
NETMASK_DEFAULT='255.255.255.0'
AGREE_DEFAULT='yes'

echo ""
echo "|==========================================================|"
echo "|**********************************************************|"
echo "|                       DHCP CONF                          |"
echo "|   Welcome to this configuration Script for DHCP Server.  |"
echo "|                                                          |"
echo "|**********************************************************|"
echo "|==========================================================|"
echo ""
echo "CTRL-C to leave"
echo ""

read -p "WARNING. The file $PATH_DHCP_FILE will be edit. Do you want to continue ? [yes] " AGREE
AGREE=${AGREE:-$AGREE_DEFAULT}

if [ "$AGREE" != 'yes' ]
  then echo "Stop script."
  exit
fi

echo ""
echo "Please, fill the following informations (be care about the syntax)"
read -p "Network IP [192.168.1.0]: " IP
read -p "Netmask [255.255.255.0]: " NETMASK
read -p "IP Range [192.168.1.50 192.168.1.100]: " IP_RANGE
IP=${IP:-$IP_DEFAULT}
NETMASK=${NETMASK:-$NETMASK_DEFAULT}
IP_RANGE=${IP_RANGE:-$IP_RANGE_DEFAULT}

echo ""
echo "--DHCP Configuration--"
echo "----Append configuration to $PATH_DHCP_FILE----"
echo "

# ============================
# *DCHP Script configurations*
# ***$DATE***
subnet $IP netmask $NETMASK {
  range $IP_RANGE;
  deny unknown-clients;
}

include \"$PATH_DHCP_TRUST_FILE\";
# ============================
" >> $PATH_DHCP_FILE

echo ""
ADD_MAC_DEFAULT='yes'

echo "# ***$DATE***" >> $PATH_DHCP_TRUST_FILE
while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
    studenMac=$(echo $line| cut -d' ' -f 1)
    student=$(echo $line| cut -d' ' -f 2)

    echo "Studen mac : $studenMac"
    echo "Studen host : $student"
    echo "Add $student with $studenMac"
    echo "host $student { hardware ethernet $studenMac; }" >> $PATH_DHCP_TRUST_FILE
done < "$PATH_STUDENTS_FILE"

编辑PATH_DHCP_FILE,PATH_DHCP_TRUST_FILE和PATH_STUDENTS_FILE变量,并在脚本询问信息时完成。

从我这边的测试文件中有结果:

对于文件test.conf(/etc/dhcpd.conf)

# ============================
# *DCHP Script configurations*
# ***2018-03-19 17:36***
subnet  netmask 255.255.255.0 {
  range 192.168.1.50 192.168.1.100;
  deny unknown-clients;
}

include "./trust_host";
# ============================

并在文件trust_host中:

# ***2018-03-19 18:44***
host Prajwal { hardware ethernet 94:92:66:38:a6:79; }
host Prajwal { hardware ethernet 30:e1:71:c9:1b:43; }
host Unique { hardware ethernet 94:65:2d:23:a8:4d; }
host Anup { hardware ethernet 84:8f:69:ca:15:00; }
host Anup { hardware ethernet 88:83:22:eb:1e:41; }

小心,我不处理重复。

希望对您有所帮助。


谢谢,它将对我有帮助:)将很快对其进行测试并提供反馈
震后

1

您可以使用限制地址池deny unknown-clients;。这样,只有出现在host声明中的MAC地址才会收到IP。

而且您无需fixed-addresshost声明中指定a 。

一个最小(未经测试)的示例可能看起来像这样

subnet 192.168.0.0 netmask 255.255.0.0 {
    # option routers 192.168.1.1;
    # option ... etc.

    pool {
            range 192.168.100.1 192.168.101.254;
            deny unknown-clients;
    }
}

include "/etc/dhcpd/known-hosts.conf";

在“ /etc/dhcpd/known-hosts.conf”中,使用MAC地址列表中的脚本可以很容易地生成它:

host h1 { hardware ethernet 00:09:b0:48:ca:ec; }
host h2 { hardware ethernet 00:80:88:11:e0:9f; }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.