Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# YazDHCP

## v1.2.3
## v1.2.4

### Updated on 2025-Nov-07
### Updated on 2025-Nov-14

## About

Expand Down
173 changes: 117 additions & 56 deletions YazDHCP.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
## Forked from https://github.com/jackyaz/YazDHCP ##
## ##
##########################################################
# Last Modified: 2025-Nov-07
# Last Modified: 2025-Nov-14
#---------------------------------------------------------

#############################################
Expand All @@ -29,8 +29,8 @@

### Start of script variables ###
readonly SCRIPT_NAME="YazDHCP"
readonly SCRIPT_VERSION="v1.2.3"
readonly SCRIPT_VERSTAG="25110720"
readonly SCRIPT_VERSION="v1.2.4"
readonly SCRIPT_VERSTAG="25111400"
SCRIPT_BRANCH="develop"
SCRIPT_REPO="https://raw.githubusercontent.com/AMTM-OSR/$SCRIPT_NAME/$SCRIPT_BRANCH"
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME.d"
Expand Down Expand Up @@ -109,7 +109,7 @@ readonly NVRAM_3004_DHCPvar_RegExp="dhcp_staticlist=[<]?${MACaddr_RegEx}>.+"
readonly NVRAM_3006_DHCPvar_RegExp0="dhcpres[1-9][0-9]?_rl="
readonly NVRAM_3006_DHCPvar_RegExp1="${NVRAM_3006_DHCPvar_RegExp0}<${MACaddr_RegEx}>.+"

readonly addnDHCP_HostNames=false
readonly addnDHCP_HostNames=true
readonly guestNetInfoJSfileName="GuestNetworkSubnetInfo.js"
readonly guestNetInfoJSfilePath="${SCRIPT_DIR}/$guestNetInfoJSfileName"
readonly dhcpGuestNetAllowVarKey="allowGuestNet_IPaddr_Reservations"
Expand Down Expand Up @@ -1751,10 +1751,11 @@ _CleanUp_DNSMasqConfigFiles_()
then return
fi
local gnInfoStr gnListOfIFaces="" gnIFaceName
local staticAddRegExp optionAddRegExp configAddPrefix
local confAddFBKUP configFName configAddFile
local staticAddRegExp optionAddRegExp hostsnAddRegExp
local confAddFBKUP configFName configAddFile configAddPrefix

configAddPrefix="${JFFS_Configs_Dir}/dnsmasq-"
hostsnAddRegExp="addn-hosts=${SCRIPT_DIR}/.hostnames_"
staticAddRegExp="dhcp-hostsfile=${SCRIPT_DIR}/.staticlist_"
optionAddRegExp="dhcp-optsfile=${SCRIPT_DIR}/.optionslist_"

Expand All @@ -1775,12 +1776,13 @@ _CleanUp_DNSMasqConfigFiles_()
then continue
fi
configAddFile="$(echo "$confAddFBKUP" | grep -oE "${configAddPrefix}.*.conf.add")"
if ! grep -qE "^($staticAddRegExp|$optionAddRegExp).*" "$confAddFBKUP"
if ! grep -qE "^($staticAddRegExp|$optionAddRegExp|$hostsnAddRegExp).*" "$confAddFBKUP"
then
_MoveOrDeleteFile_ "$confAddFBKUP"
continue
fi
## Remove unused YazDHCP custom lines ##
sed -i "\\~^${hostsnAddRegExp}.*~d" "$confAddFBKUP"
sed -i "\\~^${staticAddRegExp}.*~d" "$confAddFBKUP"
sed -i "\\~^${optionAddRegExp}.*~d" "$confAddFBKUP"
_MoveOrDeleteFile_ "$confAddFBKUP"
Expand All @@ -1801,9 +1803,10 @@ _CleanUp_DNSMasqConfigFiles_()
fi
configAddFile="${JFFS_Configs_Dir}/${configFName}.add"
if [ ! -s "$configAddFile" ] || \
! grep -qE "^($staticAddRegExp|$optionAddRegExp).*" "$configAddFile"
! grep -qE "^($staticAddRegExp|$optionAddRegExp|$hostsnAddRegExp).*" "$configAddFile"
then
## Remove unused YazDHCP custom lines ##
sed -i "\\~^${hostsnAddRegExp}.*~d" "$configFPATH"
sed -i "\\~^${staticAddRegExp}.*~d" "$configFPATH"
sed -i "\\~^${optionAddRegExp}.*~d" "$configFPATH"
continue
Expand All @@ -1813,14 +1816,15 @@ _CleanUp_DNSMasqConfigFiles_()
then
for gnIFaceName in $gnListOfIFaces
do
if grep -qE "^($staticAddRegExp|$optionAddRegExp)$gnIFaceName #" "$configAddFile"
if grep -qE "^($staticAddRegExp|$optionAddRegExp|$hostsnAddRegExp)$gnIFaceName #" "$configAddFile"
then
isConfigAddFileOK=true ; break
fi
done
fi
"$isConfigAddFileOK" && continue
## Remove unused YazDHCP custom lines ##
sed -i "\\~^${hostsnAddRegExp}.*~d" "$configFPATH"
sed -i "\\~^${staticAddRegExp}.*~d" "$configFPATH"
sed -i "\\~^${optionAddRegExp}.*~d" "$configFPATH"
dnsmasqConfigCHANGED=true
Expand Down Expand Up @@ -1879,7 +1883,7 @@ Auto_DNSMASQ()
ADDN_HostsFilePath="$SCRIPT_DIR/.hostnames"
ADDN_HostsComntTag="${SCRIPT_NAME}_hostnames"
ADDN_HostsDirctive="addn-hosts=$ADDN_HostsFilePath #${ADDN_HostsComntTag}#"
if ! "$addnDHCP_HostNames" || "$doIFaceDel"
if "$doIFaceDel"
then rm -f "$ADDN_HostsFilePath"
fi
#-----------------------------------------#
Expand Down Expand Up @@ -1964,7 +1968,7 @@ Auto_DNSMASQ()
#----------------#
ADDN_HostsComntTag="${SCRIPT_NAME}_hostnames_$gnIFaceName"
ADDN_HostsDirctive="addn-hosts=$ADDN_HostsFilePath #${ADDN_HostsComntTag}#"
if ! "$addnDHCP_HostNames" || "$doIFaceDel"
if "$doIFaceDel"
then rm -f "$ADDN_HostsFilePath"
fi
#-----------------------------------------#
Expand Down Expand Up @@ -3488,23 +3492,34 @@ _CIDR_IPaddrBlockContainsIPaddr_()
}

##----------------------------------------##
## Modified by Martinski W. [2025-Sep-05] ##
## Modified by Martinski W. [2025-Oct-19] ##
##----------------------------------------##
Update_Hostnames_MainLAN()
{
local theMACaddr theIPaddr4 theHostName theDNSaddr theIPaddr3
local theMACaddr theIPaddr4 theHostName theDNSaddr
local theMACaddrTag theIPaddr3
local LAN_IPaddr3="$(echo "$mainLAN_IPaddr" | cut -d'.' -f1-3)"

while IFS=',' read -r theMACaddr theIPaddr4 theHostName theDNSaddr
do
if [ "$theMACaddr" = "MAC" ] || \
[ -z "$theHostName" ] || [ -z "$theIPaddr4" ]
if [ "$theMACaddr" = "MAC" ] || [ -z "$theHostName" ] || \
[ -z "$theMACaddr" ] || [ -z "$theIPaddr4" ] || \
! echo "$theMACaddr" | grep -qE "^${MACaddr_RegEx}$" || \
! echo "$theIPaddr4" | grep -qE "^${IPv4privtx_RegEx}$"
then continue
fi
theMACaddrTag="#${theMACaddr}#"

if [ -s "$hostNamesFilePATH" ] && \
grep -q "$theMACaddrTag" "$hostNamesFilePATH"
then continue #Prevent Duplicates#
fi

theIPaddr3="$(echo "$theIPaddr4" | cut -d'.' -f1-3)"
if [ "$theIPaddr3" = "$LAN_IPaddr3" ]
if [ "$theIPaddr3" = "$LAN_IPaddr3" ] || \
_CIDR_IPaddrBlockContainsIPaddr_ "$mainNET_CIDR" "$theIPaddr4"
then
echo "$theIPaddr4 $theHostName" >> "$hostNamesFilePATH"
echo "$theIPaddr4 $theHostName $theMACaddrTag" >> "$hostNamesFilePATH"
fi
done < "$SCRIPT_CONF"
}
Expand All @@ -3515,24 +3530,27 @@ Update_Hostnames_MainLAN()
Update_Hostnames_GuestNet()
{
local gnExistingMD5 gnUpdatedMD5 theIPaddr3
local gnInfoStr gnListOfIFaces gnIFaceVarStr
local gnIFaceName gnStartIPaddr4 gnStartIPaddr3
local theMACaddr theIPaddr4 theHostName theDNSaddr
local hostNamesFileGNET hostNamesFileBKUP gnIFaceDelEntry
local gnInfoStr gnIFaceVarStr gnListOfIFaces=""
local gnIFaceName gnStartIPaddr4 gnStartIPaddr3 gnSubnet_CIDR
local theMACaddr theIPaddr4 theHostName theDNSaddr gnIFaceDelEntry
local hostNamesFileGNET hostNamesFileBKUP theMACaddrTag retCode

gnInfoStr="$(_Get_GuestNetwork_SubnetInfo_)"
if [ "${#gnInfoStr}" -eq 0 ]
then return 1
if [ "${#gnInfoStr}" -gt 0 ]
then
gnListOfIFaces="$(echo "$gnInfoStr" | cut -d'=' -f1 | cut -d'_' -f2)"
fi
gnListOfIFaces="$(echo "$gnInfoStr" | cut -d'=' -f1 | cut -d'_' -f2)"
if [ -z "$gnListOfIFaces" ]
then return 1
then
CleanUp_Hostnames_GuestNet
return 1
fi

if ! "$(_AllowGuestNetwork_IP_Reservations_ check)"
then gnIFaceDelEntry=true
else gnIFaceDelEntry=false
fi
retCode=1

for gnIFaceName in $gnListOfIFaces
do
Expand All @@ -3544,6 +3562,7 @@ Update_Hostnames_GuestNet()
then
if [ -s "$hostNamesFileGNET" ]
then
retCode=0
Print_Output true "DHCP hostname list for Guest Network [$gnIFaceName] was removed" "$WARN" oneline
if [ "$fwInstalledBaseVers" -ge 3006 ]
then RESTART_DNSMASQ=true
Expand All @@ -3562,26 +3581,39 @@ Update_Hostnames_GuestNet()
gnIFaceVarStr="GNIFACE_${gnIFaceName}="
gnStartIPaddr4="$(echo "$gnInfoStr" | grep -E "^${gnIFaceVarStr}.*" | cut -d'=' -f2 | cut -d',' -f1)"
gnStartIPaddr3="$(echo "$gnStartIPaddr4" | cut -d'.' -f1-3)"
gnSubnet_CIDR="$(echo "$gnInfoStr" | grep -E "^${gnIFaceVarStr}.*" | cut -d'=' -f2 | cut -d',' -f3)"

while IFS=',' read -r theMACaddr theIPaddr4 theHostName theDNSaddr
do
if [ "$theMACaddr" = "MAC" ] || \
[ -z "$theHostName" ] || [ -z "$theIPaddr4" ]
if [ "$theMACaddr" = "MAC" ] || [ -z "$theHostName" ] || \
[ -z "$theMACaddr" ] || [ -z "$theIPaddr4" ] || \
! echo "$theMACaddr" | grep -qE "^${MACaddr_RegEx}$" || \
! echo "$theIPaddr4" | grep -qE "^${IPv4privtx_RegEx}$"
then continue
fi
theMACaddrTag="#${theMACaddr}#"

if [ -s "$hostNamesFilePATH" ] && \
[ "$fwInstalledBaseVers" -lt 3006 ] && \
grep -q "$theMACaddrTag" "$hostNamesFilePATH"
then continue #Prevent Duplicates#
fi

theIPaddr3="$(echo "$theIPaddr4" | cut -d'.' -f1-3)"
if [ "$theIPaddr3" = "$gnStartIPaddr3" ]
if [ "$theIPaddr3" = "$gnStartIPaddr3" ] || \
_CIDR_IPaddrBlockContainsIPaddr_ "$gnSubnet_CIDR" "$theIPaddr4"
then
echo "$theIPaddr4 $theHostName" >> "$hostNamesFileGNET"
echo "$theIPaddr4 $theHostName $theMACaddrTag" >> "$hostNamesFileGNET"
fi
done < "$SCRIPT_CONF"

if [ -s "$hostNamesFileGNET" ]
if [ "$(_GetFileSizeBytes_ "$hostNamesFileGNET")" -ge 3 ]
then
gnUpdatedMD5="$(md5sum "$hostNamesFileGNET" | awk '{print $1}')"
fi
if [ -n "$gnUpdatedMD5" ] && [ "$gnExistingMD5" != "$gnUpdatedMD5" ]
then
retCode=0
Print_Output true "DHCP hostname list for Guest Network [$gnIFaceName] updated successfully" "$PASS" oneline
if [ "$fwInstalledBaseVers" -ge 3006 ]
then RESTART_DNSMASQ=true
Expand All @@ -3590,6 +3622,7 @@ Update_Hostnames_GuestNet()
then
if [ -s "$hostNamesFileBKUP" ]
then
retCode=0
Print_Output true "DHCP hostname list for Guest Network [$gnIFaceName] was removed." "$WARN" oneline
if [ "$fwInstalledBaseVers" -ge 3006 ]
then RESTART_DNSMASQ=true
Expand All @@ -3601,11 +3634,44 @@ Update_Hostnames_GuestNet()
fi
Print_Output true "DHCP hostname list for Guest Network [$gnIFaceName] remains unchanged" "$PASS" oneline
fi
if [ "$fwInstalledBaseVers" -lt 3006 ] && [ -s "$hostNamesFileGNET" ]
if [ -s "$hostNamesFileGNET" ] && \
[ "$fwInstalledBaseVers" -lt 3006 ]
then
cat "$hostNamesFileGNET" >> "$hostNamesFilePATH"
fi
"$delBkupCopy" && rm -f "$hostNamesFileBKUP"
if [ "$(_GetFileSizeBytes_ "$hostNamesFileGNET")" -lt 3 ]
then rm -f "$hostNamesFileGNET"
fi
done

CleanUp_Hostnames_GuestNet
return "$retCode"
}

##-------------------------------------##
## Added by Martinski W. [2025-Sep-13] ##
##-------------------------------------##
CleanUp_Hostnames_GuestNet()
{
local gnInfoStr gnIFaceName gnListOfIFaces=""
local hostNamesRegExp hostNamesFPath

gnInfoStr="$(_Get_GuestNetwork_SubnetInfo_)"
if [ "${#gnInfoStr}" -gt 0 ]
then
gnListOfIFaces="$(echo "$gnInfoStr" | cut -d'=' -f1 | cut -d'_' -f2)"
fi

hostNamesRegExp="${SCRIPT_DIR}/.hostnames_*"
for hostNamesFPath in $(ls -1 $hostNamesRegExp 2>/dev/null)
do
gnIFaceName="$(basename "$hostNamesFPath" | cut -d'_' -f2)"
if ! echo "$gnIFaceName" | grep -qE "^${guestNetIFaces0RegExp}$" || \
echo "${gnListOfIFaces:=NONE}" | grep -qw "$gnIFaceName"
then continue
fi
rm -f "$hostNamesFPath"
done
}

Expand Down Expand Up @@ -3763,7 +3829,8 @@ Update_StaticList_GuestNet()
fi
Print_Output true "DHCP IP address reservation list for Guest Network [$gnIFaceName] remains unchanged" "$PASS" oneline
fi
if [ "$fwInstalledBaseVers" -lt 3006 ] && [ -s "$staticListFileGNET" ]
if [ -s "$staticListFileGNET" ] && \
[ "$fwInstalledBaseVers" -lt 3006 ]
then
cat "$staticListFileGNET" >> "$staticListFilePATH"
fi
Expand Down Expand Up @@ -3949,7 +4016,8 @@ Update_OptionsList_GuestNet()
fi
Print_Output true "DHCP options list for Guest Network [$gnIFaceName] remains unchanged" "$PASS" oneline
fi
if [ "$fwInstalledBaseVers" -lt 3006 ] && [ -s "$optionsListFileGNET" ]
if [ -s "$optionsListFileGNET" ] && \
[ "$fwInstalledBaseVers" -lt 3006 ]
then
cat "$optionsListFileGNET" >> "$optionsListFilePATH"
fi
Expand Down Expand Up @@ -3990,56 +4058,49 @@ CleanUp_OptionsList_GuestNet()
}

##----------------------------------------##
## Modified by Martinski W. [2025-Sep-05] ##
## Modified by Martinski W. [2025-Nov-14] ##
##----------------------------------------##
Update_Hostnames()
{
local lanExistingMD5="" lanUpdatedMD5="" msgTagStr=""
local hostNamesFilePATH="$SCRIPT_DIR/.hostnames"
local hostNamesFileORIG="${hostNamesFilePATH}.BKUP"
local hostNamesFileBKUP="${hostNamesFilePATH}.BKUP"

#-----------------------------------------------------------------
# The Hostnames file is no longer needed because the information
# is now included/embedded in the "staticlist" file.
#-----------------------------------------------------------------
if ! "$addnDHCP_HostNames"
then
rm -f "$hostNamesFilePATH" "$hostNamesFileORIG"
return 0
fi
##OFF## ! "$addnDHCP_HostNames" && return 0 ##OFF##

if [ -s "$hostNamesFilePATH" ]
then
lanExistingMD5="$(md5sum "$hostNamesFilePATH" | awk '{print $1}')"
mv -f "$hostNamesFilePATH" "$hostNamesFileORIG"
mv -f "$hostNamesFilePATH" "$hostNamesFileBKUP"
fi
printf "" > "$hostNamesFilePATH"

Update_Hostnames_MainLAN
##OFF## Update_Hostnames_GuestNet ##OFF##
Update_Hostnames_GuestNet
if [ $? -eq 0 ] || "$(_AllowGuestNetwork_IP_Reservations_ check)"
then msgTagStr="for main LAN [$mainLAN_IFname] "
fi

if [ -s "$hostNamesFilePATH" ]
if [ "$(_GetFileSizeBytes_ "$hostNamesFilePATH")" -ge 3 ]
then
lanUpdatedMD5="$(md5sum "$hostNamesFilePATH" | awk '{print $1}')"
fi

if "$(_AllowGuestNetwork_IP_Reservations_ check)"
then msgTagStr="for main LAN [$mainLAN_IFname] "
fi

if [ -n "$lanUpdatedMD5" ] && [ "$lanExistingMD5" != "$lanUpdatedMD5" ]
then
Print_Output true "DHCP hostname list ${msgTagStr}updated successfully" "$PASS" oneline
RESTART_DNSMASQ=true
elif [ -n "$lanUpdatedMD5" ]
elif [ -n "$lanUpdatedMD5" ] #NO Change#
then
if [ -s "$hostNamesFileORIG" ]
then cp -fp "$hostNamesFileORIG" "$hostNamesFilePATH"
if [ "$(_GetFileSizeBytes_ "$hostNamesFileBKUP")" -ge 3 ]
then cp -fp "$hostNamesFileBKUP" "$hostNamesFilePATH"
fi
"$verboseMode" && \
Print_Output true "DHCP hostname list ${msgTagStr}remains unchanged" "$PASS" oneline
fi
"$delBkupCopy" && rm -f "$hostNamesFileORIG"
"$delBkupCopy" && rm -f "$hostNamesFileBKUP"
if [ "$(_GetFileSizeBytes_ "$hostNamesFilePATH")" -lt 3 ]
then rm -f "$hostNamesFilePATH"
fi
}

##----------------------------------------##
Expand Down