Last active
April 11, 2024 01:38
-
-
Save fabiolimace/353907cf76cd029d201032bff5d1fa44 to your computer and use it in GitHub Desktop.
Block sites in /etc/hosts
This file contains 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
#!/usr/bin/bash | |
# | |
# Removes a block to /etc/hosts. | |
# | |
# USAGE: | |
# | |
# $ block-site-add.sh EXAMPLE.COM | |
# 0.0.0.0 example.com | |
# 0.0.0.0 www.example.com | |
# | |
SITE="${1}"; | |
SITE="${SITE,,}"; | |
SITE="${SITE#www.}"; | |
ADDR="0.0.0.0"; | |
FILE="/etc/hosts"; | |
REGEX="^#?($ADDR\s+(www\.)?$SITE)$"; | |
if [[ ! "$SITE" =~ .*\..* || -z "$SITE" ]]; | |
then | |
echo "Inform a site, for example: example.com" | |
exit 1; | |
fi; | |
if [[ "`grep -E --count "$REGEX" "$FILE"`" -eq 0 ]]; | |
then | |
sudo bash -c "echo '$ADDR $SITE' >> '$FILE'"; | |
sudo bash -c "echo '$ADDR www.$SITE' >> '$FILE'"; | |
grep -E "${REGEX}" "$FILE"; | |
else | |
echo "Already exists."; | |
exit 1; | |
fi; |
This file contains 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
#!/usr/bin/bash | |
# | |
# Removes a block from /etc/hosts. | |
# | |
# USAGE: | |
# | |
# $ block-site-remove.sh EXAMPLE.COM | |
# 0.0.0.0 example.com | |
# 0.0.0.0 www.example.com | |
# | |
SITE="${1}"; | |
SITE="${SITE,,}"; | |
SITE="${SITE#www.}"; | |
ADDR="0.0.0.0"; | |
FILE="/etc/hosts"; | |
REGEX="^#?($ADDR\s+(www\.)?$SITE)$"; | |
if [[ ! "$SITE" =~ .*\..* || -z "$SITE" ]]; | |
then | |
echo "Inform a site, for example: example.com" | |
exit 1; | |
fi; | |
if [[ "`grep -E --count "$REGEX" "$FILE"`" -eq 0 ]]; | |
then | |
echo "Not found."; | |
exit 1; | |
else | |
grep -E "$REGEX" "$FILE"; | |
sudo sed -E -i "/$REGEX/d" "$FILE"; | |
fi; |
This file contains 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
#!/usr/bin/bash | |
# | |
# Toggles a block entry in /etc/hosts, i.e. inserts or removes the pound symbol for a block entry. | |
# | |
# You can inform EXAMPLE or EXAMPLE.COM as parameter. | |
# | |
# List all block entries if no parameter is given. | |
# | |
# USAGE: | |
# | |
# $ block-site.sh EXAMPLE (unblock) | |
# #0.0.0.0 example.com | |
# #0.0.0.0 www.example.com | |
# $ | |
# $ block-site.sh EXAMPLE (block again) | |
# 0.0.0.0 example.com | |
# 0.0.0.0 www.example.com | |
# | |
# $ block-site.sh ALL (unblock all sites in the list) | |
# $ block-site.sh ALL (block all sites in the list again) | |
# | |
SITE="$1"; | |
SITE="${SITE,,}"; | |
FILE="/etc/hosts"; | |
ADDR="0\.0\.0\.0"; | |
REGEX="($ADDR\s+.*$SITE.*)"; | |
if [[ -z "$SITE" ]]; then | |
echo "List of sites:" | |
grep -E "$REGEX" "$FILE" | sed -E "s/^(#?$ADDR)/\1/" | sed -E 's/\s+/ /g' | sed -E 's/^0/ 0/' | sed -E 's/www/zzz/' | sort | sed -E 's/zzz/www/'; | |
exit 0; | |
fi; | |
if [[ "$SITE" == "all" ]]; then | |
REGEX="${REGEX/$SITE/}" | |
fi; | |
if [[ "`grep -E --count "^#$REGEX$" "$FILE"`" -gt 0 ]]; then | |
sudo sed -E -i "s/^#$REGEX$/\1/" "$FILE" | |
else | |
sudo sed -E -i "s/^$REGEX$/#\1/" "$FILE" | |
fi; | |
grep -E "^#?$REGEX" "$FILE" | sed -E 's/www/zzz/' | sort | sed -E 's/zzz/www/' || (echo "Not found."; exit 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Show the full block list:
Unblock FACEBOOK:
Block FACEBOOK again:
Add AMAZON.COM to the block list:
Show the full block list:
Remove FACEBOOK.COM from the block list:
Show the full block list: