Home > Networking Solutions > Enterprise/Data Center Networking Solutions > Enterprise SONiC Networking Solutions > Guides > Zero Touch Deployment of Open Dell Switches with SONiC > Create callback cgi-script for webserver
This step is optional.
Note: The use of this script is optional. If you do not want this functionality, omit this step, and the hostname on the switch will not be configured in the same way as the hostname in the dhcpd.conf file.
The following script is used on the webserver to create a file when the script is requested. The output file receives its name from the DHCP IP address that is offered to the switch. Within this file, the desired hostname is written. The desired hostname is extracted from the dhcpd.conf file. The SONiC switch will request the callback file and use the desired hostname to configure it on the switch as a final step. After this step, the ZTP staging has finished. The following figure shows the flow of this functionality.
As an example, for switch IP address 1.2.3.4 with host entry my-dell-switch in the dhcpd.conf file, the callback output file written on the server would be:
peter@pp-ztd:/tftpboot/sonic$ cat /tftpboot/callback/1.2.3.4
my-dell-switch
peter@pp-ztd:/tftpboot/sonic$
The corresponding entry in /etc/dhcp/dhcpd.conf would be:
group {
host my-dell-switch {
hardware ethernet 4c:76:25:e8:29:c0; #mac of mgmt eth interface
fixed-address 1.2.3.4;
option ztp_json_url "http://192.168.240.101/tftpboot/sonic/ztp/ztp_data_with_firmware.json";
}
}
The cgi-script needs to be created and pushed into the HTTPD docker container. First, you create the script. You can store it to your home directory for convenience. After you store it in your container, you do not have to keep it in your home folder. Create the script and set the execute attribute:
touch callback.sh && chmod a+x callback.sh
Copy and paste the following into the file:
#!/bin/bash
DHCPD_CONF=/etc/dhcp/dhcpd.conf
TFTPBOOT=/var/www/html/tftpboot
CALLBACK=callback
SED=`type -p sed`
GREP=`type -tP grep`
## Silent run of SED
## Find lines matching host & start with leading spaces
## Remove these leading characters & Store what is left in the hold space
## Find fixed-address with the requesting IP address of the device
## x: Swap pattern and hold space
## p: Print pattern space
## q: Stop processing
## Second SED line is to strip {
$SED -n '/^ *host /{s///;h};/^ *fixed-address '"$REMOTE_ADDR"'/{x;p}' $DHCPD_CONF > $TFTPBOOT/$CALLBACK/$REMOTE_ADDR
$SED -i 's/ {//g' $TFTPBOOT/$CALLBACK/$REMOTE_ADDR
## These lines are needed to echo back to apache server and avoid errors
echo Content-type: text/html
echo
Copy the callback.sh into the container. Note the container id:
docker ps | grep “enonicio/apache2”
<snip>
8b860abfb41f
Upload the callback.sh script to the webserver cgi-bin folder:
docker cp callback.sh 8b860abfb41f:/usr/lib/cgi-bin/
Be sure to make the script permanent to your webserver image. It is beyond the scope of this whitepaper to cover specific details. See documentation about Docker images and container management.
If the switch requests http://192.168.240.101/cgi-bin/callback.sh from the provisioningsscript postscript_simple.sh, the webserver runs the callback.sh script and saves the desired hostname into the file with the IP address as filename.