Category filter

Script to Allow/Block Websites on Mac

Web content filtering is an effective access control mechanism to allow/deny web access on the devices. Blocking specific websites or URLs on the browser helps an administrator prevent the user from spending effective business hours on irrelevant web pages and secure the device from malicious websites. However, if you want to limit web access to particular URLs, you can get it done by whitelisting them. This article enables you to perform web content filtering on macOS devices over the air using scripts deployed via the Execute Custom Script action.

Disclaimer:

The Sample Scripts provided below are adapted from third-party Open-Source sites.

Script to block access to specific websites

The Hosts files are an essential tool that enables you to modify how the device views a particular website. For example, we can use the Hosts files present on the device to block access to specific URLs. The following script helps you to edit the Hosts files on a device to block an unwanted website.

You can replace www.facebook.com with the necessary URL.

Notes:

  • There can be multiple versions for a single website. Therefore, ensure you include all variations —for instance, facebook.com, https://www.facebook.com, https://m.facebook.com etc.
  • It is recommended that you verify the domains or subdomains that may/may not be accessed from when you specify a URL. Hence, include the web address in the script accordingly.

Script to allow access to the blocked websites

Just in case you want to permit access to the blocked websites on the Hosts files you can do it using the following script:

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository