What's new

Dnsmasq configuration

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Denna

Senior Member
1) Is it possible to view the configuration of dnsmasq after /jffs/scripts/dnsmasq.postconf runs ?

2) What is wrong with the following dnsmasq.postconf script ? Running this script from the command line returns ...
Code:
./dnsmasq.postconf: line 9: : No such file or directory
Code:
#!/opt/bin/bash

PRIV_VLAN=(eth1.1 eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(3 2 1 0)
VLAN_ID=(10 20 30 40)

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
   cat <<EOF >>"$1"
   interface=${PRIV_VLAN[i]}
   dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
   dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
   dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
   EOF
done
 
Last edited:
1) Is it possible to view the configuration of dnsmasq ?

2) What is wrong with the following dnsmasq.postconf script ? Running this script from the command line returns ...
Code:
./dnsmasq.postconf: line 9: : No such file or directory
Code:
#!/opt/bin/bash

PRIV_VLAN=(eth1.1 eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(3 2 1 0)
VLAN_ID=(10 20 30 40)

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
   cat <<EOF >>"$1"
   interface=${PRIV_VLAN[i]}
   dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
   dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
   dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
   EOF
done
1) Yes in /etc/dnsmasq.conf
2) Have you marked it as executable +x?

Usually postconf isn't run manually, rather it is called when the dnsmasq service restarts
Code:
service restart_dnsmasq
 
I was running it to determine if there were any issues with the for loop.
 
2) What is wrong with the following dnsmasq.postconf script ? Running this script from the command line returns ...
For starters, unless bash is installed through Entware on your router, use sh instead of bash in the shebang:
Code:
#!/opt/bin/sh
Then, $1 is not defined in your script, add it with the following so the script knows what file to edit:
Code:
CONFIG=$1
source /usr/sbin/helper.sh
This is required for a *.postconf script as per the wiki.
Then replace the $1 with $CONFIG in your code.
 
@thelonelycoder,

I'm using bash due to the use of an array.

The same error is now occurring on line 10.
Code:
#!/opt/bin/bash

PRIV_VLAN=(eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(2 1 0)
VLAN_ID=(20 30 40)
CONFIG=$1
source /usr/sbin/helper.sh

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
   cat <<EOF >>"$1"
   interface=${PRIV_VLAN[i]}
   dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
   dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
   dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
   EOF
done
 
@thelonelycoder,

I'm using bash due to the use of an array.

The same error is now occurring on line 10.
Code:
#!/opt/bin/bash

PRIV_VLAN=(eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(2 1 0)
VLAN_ID=(20 30 40)
CONFIG=$1
source /usr/sbin/helper.sh

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
   cat <<EOF >>"$1"
   interface=${PRIV_VLAN[i]}
   dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
   dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
   dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
   EOF
done
I see, try to outdent the EOF, maybe that helps:
Code:
 #!/opt/bin/bash

PRIV_VLAN=(eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(2 1 0)
VLAN_ID=(20 30 40)
CONFIG=$1
source /usr/sbin/helper.sh

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
   cat <<EOF >>"$CONFIG"
interface=${PRIV_VLAN[i]}
dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
EOF
done
 
<sigh>

Are you actually passing the parameter into your script?

./dnsmasq.postconf /etc/dnsmasq.conf
 
Figured it out.

Code:
#!/opt/bin/bash

PRIV_VLAN=(eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(2 1 0)
VLAN_ID=(20 30 40)
CONFIG=/etc/dnsmasq.conf

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
cat <<EOF >>"$CONFIG"
interface=${PRIV_VLAN[i]}
dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
EOF
done
 
Figured it out.

Code:
#!/opt/bin/bash

PRIV_VLAN=(eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(2 1 0)
VLAN_ID=(20 30 40)
CONFIG=/etc/dnsmasq.conf

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
cat <<EOF >>"$CONFIG"
interface=${PRIV_VLAN[i]}
dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
EOF
done
That's because you hardcoded the reference to the config file rather than pass it as an argument, as per @ColinTaylor

Whatever works I guess
 
From the command line, running "/jffs/scripts/dnsmasq.postconf /etc/dnsmasq.conf" does update the /etc/dnsmasq.conf file.

Code:
#!/opt/bin/bash

PRIV_VLAN=(eth1.2 eth1.3 eth1.4)
SWITCH_PORT=(2 1 0)
VLAN_ID=(20 30 40)
CONFIG="$1"

for (( i=0; i<${#PRIV_VLAN[@]}; i++ )); do
cat <<EOF >>"$CONFIG"
interface=${PRIV_VLAN[i]}
dhcp-range=${PRIV_VLAN[i]},192.168.${VLAN_ID[i]}.0.2,192.168.${VLAN_ID[i]}0.254,255.255.255.0,8h
dhcp-option=${PRIV_VLAN[i]},3,192.168.${VLAN_ID[i]}.1
dhcp-option=${PRIV_VLAN[i]},6,208.67.222.222,208.67.220.220
EOF
done

However, when dnsmasq.postconf is run at bootup, the changes are not visible in "/etc/dnsmasq.conf".
 
... However, when dnsmasq.postconf is run at bootup, the changes are not visible in "/etc/dnsmasq.conf".
I think you could do this entirely with Ash script (the default shell script: /bin/sh). The firmware would be the only dependency, no Entware-ng in this case.

If you ever need functionality not available in Ash shell, try adding Awk script (/usr/bin/awk). See this example for how I used Awk to implement some script functions. This is just an example for how one could extend the default shell with Awk scripts. You don't actually need Awk for your Dnsmasq auto-configuration because what you're trying to do is very easy.
https://www.snbforums.com/threads/block-specific-ip-using-iptables.37614/#post-342983
 
@ColinTaylor,

What do you mean "for once" ?​

Whilst it is very useful/flexible to be able to use scripting to modify configs via the .postconf method, since the VLAN configurations are known in advance, why not simply hard-code your required three VLANs in /jffs/configs/dnsmasq.conf.add thereby removing any Entware dependencies? (and totally eliminates possible scripting logic failures!;) )
 
Last edited:
@Martineau, @Fitz Mutch and @Jack Yaz,

Thanks to @Jack Yaz for pointing out the bash/Entware delay.

Replaced bash with sh.

@Martineau - I didn't want to hardcode any information so the script could remain flexible by changing environmental variables in one place. The variables are used by more than one script.

@Fitz Mutch - Thanks for the awk option suggestion.
 
Last edited:

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top