dhcp-restart script sed error

Hi,
Ref: Programming eMMC with USB for OSD335x (AM335x System in Package) | Octavo Systems
Section 4.2.3: Setup DHCP Server on Host Computer

I understand the dhcp-restart script is suppose to add the name of the new interface to the INTERFACES variable of the /etc/default/isc-dhcp-server configuration file. In my case, I found that the name was not being added to the isc-dhcp-server file because the ‘sed’ command was failing.

# how to duplicate
# open a terminal
# check current the INTERFACES lines in isc-dhcp-server file
cat /etc/default/isc-dhcp-server
# snippet from result
...
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#	Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACESv4=""
INTERFACESv6=""
# manually run the sed command
# replace '$IFACE' with 'test' (no single quotes)
sudo sed -i "/INTERFACES/ s#"$# test"#g" /etc/default/isc-dhcp-server
sed: -e expression #1, char 16: unterminated `s' command
# the command fails with unterminated 's' command
# Here is my fix
# replace the '#' terminator character(s) with ':'
# escape the two inside " double quote characters by adding '\' in front 
sudo sed -i "/INTERFACES/ s#"$# test"#g" /etc/default/isc-dhcp-server
# command should execute without error

# now recheck current the INTERFACES lines in isc-dhcp-server file
cat /etc/default/isc-dhcp-server
# snippet from new result
...
INTERFACESv4=" test"
INTERFACESv6=" test"

Adding ’ test’ to the isc-dhcp-server file shouldn’t cause a problem, but you should edit the file and delete the ’ test’ entries.

Here is my complete dhcp-restart script. You can uncomment the echo commands to help debug the problem.


#!/bin/sh
#
# Modify the /etc/default/isc-dhcp-server init script to include the new RNDIS network
# interfaces in dhcp server configuration. It will then restart the dhcp server

# Static network interfaces that should not be modified
#   *** Add any other static network interfaces on your host computer ***
if [ "$IFACE" = "gpd0" ] || [ "$IFACE" = "enp1s0" ] || [ "$IFACE" = "eno1" ] || [ "$IFACE" = "lo" ]; then
         :
else
         echo "New IFACE '"$IFACE"' found." >> /tmp/dhcp-restart.log
#        set IP address of the interface
#         ifconfig $IFACE 192.168.0.1 netmask 255.255.255.0
         ip addr add 192.168.11.1/24 dev $IFACE
         echo "Check isc-dhcp-server file for IFACE '"$IFACE"'" >> /tmp/dhcp-restart.log
         if grep -q "$IFACE" /etc/default/isc-dhcp-server
         then
                  echo "'"$IFACE"' is present - nothing to do." >> /tmp/dhcp-restart.log
                  :
         else
                  echo "Attempting to add '"$IFACE"' to isc-dhcp-server" >> /tmp/dhcp-restart.log
                  sed -i "/INTERFACES/ s:\"$: $IFACE\":g" /etc/default/isc-dhcp-server
                  if [ $? = 0 ]; then
                    echo "  successful" >> /tmp/dhcp-restart.log
                  else
                    echo "  failed!" >> /tmp/dhcp-restart.log
                  fi;
         fi;
fi;

systemctl restart isc-dhcp-server

Ooops,
I left the echo commands and the if/else uncommented. You can comment them out if you don’t want the /tmp/dhcp-restart.log file created.

Hi,
I also found a problem with the /etc/default/isc-dhcp-server file.

Here is a snippet from my isc-dhcp-server file.

...
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#	Separate multiple interfaces with spaces, e.g. "eth0 eth1".
...

Notice the second line above has “eth0 eth1” as part of the comment. I noticed that when I plugged the target board into the host PC that the new interface came up as ‘eth0’. This caused the dhcp-restart script to fail to add the new ‘eth0’ interface because the grep command found ‘eth0’ in the comment, which caused the sed command not to run.

I fixed the problem by editing isc-dhcp-server file and deleting “eth0 eth1” from the comment.

Thank you,

Hey Jack,

Appreciate your inputs! I will review and update as necessary.

Best,
Neeraj