Skip to content

Instantly share code, notes, and snippets.

@caseyWebb
Forked from rwd/port_forward.sh
Last active November 26, 2016 22:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caseyWebb/f869c3d9afab064e94b1dc9f021b89d8 to your computer and use it in GitHub Desktop.
Save caseyWebb/f869c3d9afab064e94b1dc9f021b89d8 to your computer and use it in GitHub Desktop.
PIA port forwarding with Linux ifconfig alternatives
#! /bin/bash
#
# Enable port forwarding
#
# Requirements:
# your Private Internet Access user and password as arguments
#
# Usage:
# ./port_forward.sh <user> <password>
error( )
{
echo "$@" 1>&2
exit 1
}
error_and_usage( )
{
echo "$@" 1>&2
usage_and_exit 1
}
usage( )
{
echo "Usage: `dirname $0`/$PROGRAM <user> <password>"
}
usage_and_exit( )
{
usage
exit $1
}
version( )
{
echo "$PROGRAM version $VERSION"
}
port_forward_assignment( )
{
echo 'Loading port forward assignment information..'
if [ "$(uname)" == "Linux" ]; then
if type ip >/dev/null 2>/dev/null; then
local_ip=`ip addr show tun0 | grep "inet " | head -n 1 | sed 's/^ *//g' | cut -d\ -f2 | tee /tmp/vpn_ip`
elif ifconfig tun0 | grep -oE "inet addr: "; then
local_ip=`ifconfig tun0 | grep -oE "inet addr: *10\.[0-9]+\.[0-9]+\.[0-9]+" | tr -d "a-z :" | tee /tmp/vpn_ip`
else
local_ip=`ifconfig tun0 | grep "inet " | sed 's/^ *//g' | cut -d\ -f2 | tee /tmp/vpn_ip`
fi
fi
if [ "$(uname)" == "Darwin" ]; then
local_ip=`ifconfig tun0 | grep "inet " | cut -d\ -f2|tee /tmp/vpn_ip`
fi
client_id=`cat /tmp/vpn_client_id`
json=`wget -q --post-data="user=$USER&pass=$PASSWORD&client_id=$client_id&local_ip=$local_ip" -O - 'https://www.privateinternetaccess.com/vpninfo/port_forward_assignment' | head -1`
echo $json
}
EXITCODE=0
PROGRAM=`basename $0`
VERSION=1.0
USER=$1
PASSWORD=$2
while test $# -lt 2
do
case $1 in
--usage | --help | -h )
usage_and_exit 0
;;
--version | -v )
version
exit 0
;;
*)
error_and_usage "Unrecognized option: $1"
;;
esac
shift
done
if [ ! -f /tmp/vpn_client_id ]; then
if [ "$(uname)" == "Linux" ]; then
echo head -n 100 /dev/urandom | md5sum | tr -d " -" >> /tmp/vpn_client_id
fi
if [ "$(uname)" == "Darwin" ]; then
echo head -n 100 /dev/urandom | md5 -r | tr -d " -" >> /tmp/vpn_client_id
fi
fi
port_forward_assignment
exit 0
@blaccko
Copy link

blaccko commented Nov 26, 2016

Hi,

My Transmission jail is up and running. My VPN is configured and works with OpenVPN. My firewall is configured with ipfw and works (the connection is stopped if the VPN stops). My only concern now is to port forward a port to Transmission. I'm using FreeNAS 9.10, PIA for VPN and ipfw.

I tried to use the present script (the fork) to port forward a port to Transmission (and to generate the same port each time it is executed). However, I get an error (command not found) when I try to execute the script. I've been reading a lot and I tried to install bash, as you can see per code, but it's already installed. So, I don't know what to try now to get the script working. Could you please help me with this issue?

root@transmission_1:/ # cd /usr/local/etc/openvpn root@transmission_1:/usr/local/etc/openvpn # ls 4b05e93c63684ea1a95518fcfbd8a6a04461b1d5 PIA ca.rsa.2048.crt crl.rsa.2048.pem openvpn.conf openvpn.zip password.txt port_forward.sh port_forward.sh.1 root@transmission_1:/usr/local/etc/openvpn # pkg install bash Updating FreeBSD repository catalogue... FreeBSD repository is up-to-date. All repositories are up-to-date. Checking integrity... done (0 conflicting) The most recent version of packages are already installed root@transmission_1:/usr/local/etc/openvpn # ./port_forward.sh testuname testpas sword ./port_forward.sh: Command not found. root@transmission_1:/usr/local/etc/openvpn #

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment