Created
January 22, 2026 19:54
-
-
Save alkimiadev/aa1de1fbce8d9bd11eb05da3cbef6f50 to your computer and use it in GitHub Desktop.
silly script to block X and meant to be used with a cron to limit usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # The domains we want to block | |
| DOMAINS="x.com www.x.com twitter.com www.twitter.com" | |
| # Where we point them (Localhost) | |
| REDIRECT="127.0.0.1" | |
| # The hosts file path | |
| HOSTS="/etc/hosts" | |
| # A unique comment to identify our entry | |
| MARKER="# X_BLOCK_CONTROL" | |
| # Function to block X | |
| block_x() { | |
| # Check if we've already added it to prevent duplicates | |
| if grep -q "$MARKER" "$HOSTS"; then | |
| echo "X.com is already blocked." | |
| else | |
| echo "Blocking X.com..." | |
| echo -e "$REDIRECT $DOMAINS $MARKER" >> "$HOSTS" | |
| flush_dns | |
| fi | |
| } | |
| # Function to unblock X | |
| unblock_x() { | |
| echo "Unblocking X.com..." | |
| # Remove any line containing the marker | |
| if [[ "$OSTYPE" == "darwin"* ]]; then | |
| # macOS version of sed | |
| sed -i '' "/$MARKER/d" "$HOSTS" | |
| else | |
| # Linux version of sed | |
| sed -i "/$MARKER/d" "$HOSTS" | |
| fi | |
| flush_dns | |
| } | |
| # Function to flush DNS cache so changes take effect immediately | |
| flush_dns() { | |
| echo "Flushing DNS cache..." | |
| if [[ "$OSTYPE" == "darwin"* ]]; then | |
| sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder | |
| else | |
| # Many Linux distros use systemd-resolved | |
| if command -v systemd-resolve &> /dev/null; then | |
| sudo systemd-resolve --flush-caches | |
| elif command -v service &> /dev/null; then | |
| # Try generic restart of dns service | |
| sudo service dns-clean restart &> /dev/null | |
| fi | |
| fi | |
| } | |
| # Main logic | |
| case "$1" in | |
| block) | |
| block_x | |
| ;; | |
| unblock) | |
| unblock_x | |
| ;; | |
| *) | |
| echo "Usage: $0 {block|unblock}" | |
| exit 1 | |
| ;; | |
| esac |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This can be used by
I'm personally using it with a cron job that will only unlock it between 6 and 7 pm daily