Skip to content

Instantly share code, notes, and snippets.

@OlofFredriksson
Last active October 13, 2015 21:48
Show Gist options
  • Save OlofFredriksson/4261028 to your computer and use it in GitHub Desktop.
Save OlofFredriksson/4261028 to your computer and use it in GitHub Desktop.
(Ubuntu) VPN, Firewall block
#! /bin/bash
# Functions needed for this script
is_valid_ipv4() {
local -a octets=( ${1//\./ } )
local RETURNVALUE=0
# return an error if the IP doesn't have exactly 4 octets
[[ ${#octets[@]} -ne 4 ]] && return 1
for octet in ${octets[@]}
do
if [[ ${octet} =~ ^[0-9]{1,3}$ ]]
then # shift number by 8 bits, anything larger than 255 will be > 0
((RETURNVALUE += octet>>8 ))
else # octet wasn't numeric, return error
return 1
fi
done
return ${RETURNVALUE}
}
# Connect to vpn
nmcli con up id Anon
#Get ip so we can bind it to rtorrent
read ip <<< $(/sbin/ifconfig tap 2>&1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
echo "$ip"
is_valid_ipv4 ${ip}
if [[ $? -gt 0 ]]
then
echo "Invalid IP, something is wrong the connection."
else
echo "IP is valid, lets enable firewall."
sudo ufw enable
sudo ufw deny in on eth0
sudo ufw deny out on eth0
sudo ufw status verbose
fi
# Credits
# http://www.dopefish.de/archives/1180
# http://ubuntuforums.org/showthread.php?t=1154442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment