midrange.com code scratchpad
Name:
revproxy_check_internal_host
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
01/05/2022 08:17:46 pm
IP:
Logged
Description:
This snippet uses netcat to look if port 8081 on my internal host nibbler.pocnet.net is available. Dependent on the return value of netcat, the proxy or local configuration is linked to the current directory. If a change has been made (derived by the file command), apache is reloaded. So, multiple blocks of "if echo" do trigger a reload only once in any case.

The path to cd to in line 3 is dependent on the Linux distribution used. The example is for debian based systems.
Code:
  1. #!/bin/sh
  2.  
  3. cd /etc/apache2/sites-enabled || exit 1
  4.  
  5. CHANGED=0
  6.  
  7. if echo "GET /" |nc -w 3 nibbler.pocnet.net 8081 &> /dev/null; then
  8.     if ! file -b www.pocnet.net.conf |fgrep -q nibbler; then
  9.         ln -sf ../sites-available/www.pocnet.net-nibbler.conf www.pocnet.net.conf
  10.         CHANGED=1
  11.     fi
  12. else
  13.     if ! file -b www.pocnet.net.conf |fgrep -q local; then
  14.         ln -sf ../sites-available/www.pocnet.net-local.conf www.pocnet.net.conf
  15.         CHANGED=1
  16.     fi
  17. fi
  18.  
  19. # Reload config if appropriate
  20. if [ ${CHANGED} -eq 1 ]; then
  21.     if /usr/sbin/apache2ctl configtest 2>/dev/null; then
  22.         /etc/init.d/apache2 reload >/dev/null
  23.     else
  24.         echo "$0 has not reloaded Apache due to config errors. Please fix."
  25.         exit 1
  26.     fi
  27. fi
  28.  
  29. # -EOF-
  30.  
© 2004-2019 by midrange.com generated in 0.006s valid xhtml & css