I need to modify Python script to redirect traffic for specified websites to the localhost address if we are not connected via a specified Public IP address. I'm going to launch this script on Mac OS (Big Sur or Catalina) and other Unix-based systems. It works well by I have two problems:
1) I need to modify /etc/hosts file. For example, when my script is run already and I'm going to visit a restricted website it's for an untrusted network - it's writing this domain name to /etc/hosts file with address [login to view URL], but when I'm switching my Public IP address to correct one - it's still doesn't work cause previous entry. So switching for the correct IP address should be fixed.
2) To write some kind of banner to remind the user to switch Public IP address to correct one when he is trying to connect in the wrong way.
Also I have working (almost) version of script for Ubuntu, but it doesn't work properly on Mac OS.