-
-
Save rwd/d8fe62c8360f7dcd913a to your computer and use it in GitHub Desktop.
#! /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 | |
client_id=`head -n 100 /dev/urandom | md5sum | tr -d " -"` | |
fi | |
if [ "$(uname)" == "Darwin" ]; then | |
local_ip=`ifconfig tun0 | grep "inet " | cut -d\ -f2|tee /tmp/vpn_ip` | |
client_id=`head -n 100 /dev/urandom | md5 -r | tr -d " -"` | |
fi | |
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 | |
port_forward_assignment | |
exit 0 |
Hey man, love the work you did with this script. I'm not sure if you looked at it in a while but I was curious if you could take a look at the error message I was getting when trying to run it.
Loading port forward assignment information..
wget: missing URL
Usage: wget [OPTION]... [URL]...
Try wget --help' for more options. ./port_forward.sh: line 65: --post-data=user=*******&pass=****&client_id= &local_ip=: command not found ./port_forward.sh: line 66: -O: command not found ./port_forward.sh: line 67: https://www.privateinternetaccess.com/vpninfo/port_f orward_assignment: No such file or directory ./port_forward.sh: command substitution: line 68: syntax error near unexpected t oken
|'
./port_forward.sh: command substitution: line 68: `| head -1'
Looks like the URL may have changed? Any thoughts are greatly appreciated!
Leaving this here for posterity...
If you're getting an empty 500 response, url encode your password. That fixed it for me.
This revision keeps the client_id
constant so the script always returns the same port on the same connection.
Thank you for this... I'm not an expert at shell scripting syntax and your work saved me a lot of looking stuff up to convert the old PIA script for use with ip instead of ifconfig (after installing Debian Stretch I just didn't want to go backwards).
Thanks works great. Anyway i can modify this to update sshd_config to the open port? i'd like to have a open port to ssh into my machine after a reboot or connection drop. I guess i'd need it to also email me the updated port so i can update my laptop/mobile. Unfortunately i'm not much of a coder.