Wednesday, February 12, 2014

Block local ip or a website - Linux Script

This is a simple script that uses the tcpkill to block internet access to a specific machine on a local network or a website from the entire network. When a website is blocked, you will still have access to it. If the target is blocked, they will still show an active connection in the tray.  -- Use with LAN connection works, WLAN doesn't work all of the time.

The tutorial at the bottom integrates the script into the right click menu of Openbox.

Video tutorial / in action: Here

1. In the terminal enter sudo apt-get -y install dsniff --no-install-recommends
2. Enter  nano ~/script.sh
3. Paste this in


LanInt="eth0"

function BlockConnection
{
if [ -n "$(pidof tcpkill)" ]; then killall tcpkill >/dev/null 2>/dev/null; fi  #Stop

#Option Menu
if [ -z "$(pidof tcpkill)" ]; then clear; echo; echo
echo "    Block Website/Webpage or IP Address"
echo
echo "    (1) IP   (2) Website   (3) Cancel"
echo
read -p "                 > " BlockCheck
if [[ "$BlockCheck" == "1" ]]; then clear; echo; echo; echo; read -p "       IP Address: " BlockSite     #IP
elif [[ "$BlockCheck" == "2" ]]; then clear; echo; echo; echo; read -p "       Website/Webpage: " BlockSite #Website/Webpage
elif [[ "$BlockCheck" == "3" ]]; then MainMenu
else BlockConnection; fi

#IP
if [[ "$BlockCheck" == "1" ]]; then clear; tcpkill -i $LanInt -9 host "$BlockSite"; fi

#WebSite/Webpage
if [[ "$BlockCheck" == "2" ]]; then 
if [[ "$BlockSite" == *"www."* ]]; then BlockSite="$BlockSite"; else BlockSite="www.$BlockSite"; fi #Add www.  (If needed)
clear; tcpkill -i $LanInt -9 host "$BlockSite"; fi
fi
 }

BlockConnection


4. Save the script
5. Lastly type sudo chmod +x script.sh


eth0 for wired connection or wlan0 for wireless
6. Open a terminal and enter:
sudo sh ~/script.sh

Using Openbox's menu to execute the script
1. In the terminal type in obmenu
2. Enter a name for the label. This is what show up when you right click. I used Bock Connection.
3. Add this to Execute at the bottom
 terminator --command="sudo sh ~/script.sh"
Replace ~/script.sh to the location and file name of your choice
4. Click Save at the top. Right click on the desktop, then select Block Connection to launch the script

No comments:

Post a Comment