How to configure an Apache server/crontab/bash file to allow access at certain times during the day and/or from specific IPs?

Jordan :

I am attempting an exercise given by my instructor. We were tasked with setting up a VirtualHost that could be accessed mon-fri/1-5. On top of that, we must allow access to the site from IP "XX.XX.XX.XXX" at anytime.

Current crontab:

* 13 * * 1-5 root apachectl start
* 17 * * 1-5 root apachectl stop
* * * * * root ./bash

Bash file:

currIP=$(hostname -I)
case "$currIP" in
        *XX.XX.XX.XXX*)
              ???????
;;
esac

Can someone explain to me how I am going to serve this site to the IP without serving it to everyone at once?

Thanks!

Matias Barrios :

Lets say this is your config file.

/etc/apache2/httpd.conf

Run these commands :

sudo cp /etc/apache2/httpd.conf all.conf
sudo cp /etc/apache2/httpd.conf restricted.conf

Now create this script somewhere in your system :

#!/bin/bash

[[ "$1" = "all" ]] && { ln -s --force /etc/apache2/all.conf /etc/apache2/httpd.conf ;}
[[ "$1" = "restricted" ]] && { ln -s --force /etc/apache2/restricted.conf /etc/apache2/httpd.conf ;}
sudo systemctl restart httpd

And add execution permissions to it :

sudo chmod +x /path/to/your/script.sh

Now modify /etc/apache2/restricted.conf to filter requests by IP. You can find information on that here : https://httpd.apache.org/docs/2.4/es/mod/mod_authz_core.html#require

Now set the root cron as :

sudo crontab -e

And in there add this :

* 13 * * 1-5 /path/to/your/script.sh "all"
* 17 * * 1-5 /path/to/your/script.sh "restricted"

And be happy!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=319494&siteId=1