0) Full script on gist. I made this for myself to automate a boring and long process. If you don't know what you're doing you should probably do it the 'hard' way first so you understand at least what the script is doing. It's also just for Private Internet Access (PIA) because I have PIA.
1) Tested on my: 9.3-RELEASE-p5 FreeBSD 9.3-RELEASE-p5 #1 f8ed4e8: Fri Dec 19 20:25:35 PST 2014
2) Not responsible for this losing your data, formatting your drives or your wife leaving you. This is supposed to be run inside the jail. It requires at least curl or wget to be installed. Tested with Transmission plugin jail &
3) Code should work like this:
Code:jls jexec [JAILID] tcsh cd /tmp wget --quiet --no-check-certificate -O pia.sh https://gist.githubusercontent.com/jedediahfrey/6d475dcc34c710f62a7c/raw/d9e2c8f26da0da5ba4e347df1c0210fde42884a8/pia.sh chmod +x pia.sh ./pia.sh
![]()
![]()
The end of the script should show you this:
Code:Starting openvpn. Waiting 10 seconds for OpenVPN to spin up If these are different, OpenVPN is working Old IP: 68.[x].[x].[x] New IP: 179.[x].[x].[x]
Username change, updated.
https://gist.githubusercontent.com/...f18484225b0676fe0c556e7798cc08cdc7a631/pia.sh
For some reason the script is not executing for me or something is wrong with the script (which I doubt because it was working perfectly in the past, and I doubt you changed it). This is with the new link to the script.
In transmission jail I execute the following:
cd /tmp
wget --quiet --no-check-certificate -O pia.sh https://gist.githubusercontent.com/...f18484225b0676fe0c556e7798cc08cdc7a631/pia.sh
chmod +x pia.sh
./pia.sh
Nothing happens. I can see that I downloaded the file and have changed the permissions via "ls -l" -rwxr-xr-x 1 root wheel 0 Jan 4 08:58 pia.sh
Any ideas?
Thanks
wget --quiet --no-check-certificate -O pia.sh https://gist.githubusercontent.com/jed-frey/6d475dcc34c710f62a7c/raw/fcf18484225b0676fe0c556e7798cc08cdc7a631/pia.sh
! /usr/local/bin/bash
#
# Script also based on Nodja's script at https://forums.freenas.org/index.php?threads/guide-setting-up-transmission-with-openvpn-and-pia.24566/page-10#post-248580
#
# to generate a new client id run
# head -n 100 /dev/urandom | md5 -r | tr -d " -"
# in any terminal
#
# usage: ./port_forward.sh
# note: you must install bash in the jail first "pkg install bash"
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROGRAM=`basename $0`
USER=xxxxxxx
PASSWORD=xxxxxxx
RPCUSER=xxxxxxx
RPCPASSWORD=xxxxxxx
CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
local_ip=`ifconfig tun0 | grep "inet " | cut -d\ -f2|tee /tmp/vpn_ip`
json=`wget --no-check-certificate -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`
PORTNUM=`echo $json | grep -oE "[0-9]+"`
echo $PORTNUM
transmission-remote --auth $RPCUSER:$RPCPASSWORD -p $PORTNUM
exit 0
https://www.privateinternetacccess.com/vpninfo/port_forward_assignment
No such file or directory
in response. Hi Centex,So what seems to be the concensus on getting something setup... seems there's lots of options with scripts, etc. Ideally what I'd like is a given jail to use VPN and only have access to the internet thru the VPN. Ideally have access to local network as well (for couch potato interface/etc). I'd like for it to be self serving (ie reconnect when disconnected) and disallow all internet traffic obviously when disconnected.
Thanks
#!/usr/local/bin/python2.7 import sys import socket import subprocess url = 'Sweden.privateinternetaccess.com' try: # Raise if it can't ping the server or openvpn isn't running subprocess.check_output(['service', 'openvpn', 'status']) subprocess.check_output(['ping', '-c', '1', url]) except subprocess.CalledProcessError: pass else: sys.exit() hostname, aliaslist, ipaddrlist = socket.gethostbyname_ex(url) content = ''' add 01006 allow ip from 192.168.2.0/24 to 192.168.2.0/24 keep-state ''' rule_number = 2001 for ip in ipaddrlist: content += ''' add {} allow ip from 192.168.2.0/24 to {} keep-state add {} allow ip from {} to 192.168.2.0/24 keep-state '''.format(rule_number, ip, rule_number + 1, ip) rule_number += 2 content += ''' add 04000 allow ip from 127.0.0.1 to any add 05000 allow ip from 10.0.0.0/8 to any add 05002 allow ip from any to 10.0.0.0/8 add 65534 deny ip from any to any ''' # Stop ipfw subprocess.call(['service', 'openvpn', 'stop']) subprocess.call(['service', 'ipfw', 'stop']) f = open('/sabnzbd/scripts/ipfw_rules', 'w') f.write(content) f.close() # Start ipfw subprocess.call(['service', 'ipfw', 'start']) # Check if running if 'ipfw' in subprocess.check_output(['service', '-e']): subprocess.call(['service', 'openvpn', 'start']) sys.exit()
Enter Jail via putty Edit /etc/rc.conf and add the below lines ee /etc/rc.conf firewall_enable="YES" firewall_type="/sabnzbd/scripts/ipfw_rules" Copy script file to script folder (pia.py) to /sabnzbd/Scripts (Or a different location) chmod +x /sabnzbd/scripts/pia.py (Path to pia.py) in freenas gui cron job (add Cron Jobs) Name it OpenVpn Script User root command jexec transmission_1 python2.7 /sabnzbd/scripts/pia.py Every N minute 5 Every N hour 1 Day of month 1 month check all day of week check all redirect check enabled check Ip Checking in putty wget http://smart-ip.net/myip -O - -q ; echo wget -qO- http://wtfismyip.com/text
service ipfw start
service openvpn stop
#!/usr/local/bin/bash # Cronable port forwarding script for PIA/transmission running on # FreeNAS # # Requires bash, jq (JSON parser) and curl # pkg install -y jq bash curl # Assumes tunnel is tun0 if different change below # # Your PrivateInternetAccess credentials PIA_USER=username PIA_PASS=password # Export path for when you use this in cron export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin" # echo date/time for logging echo "Transmission Port Forward $(date +%Y-%m-%d-%H:%M:%S)" get_new_port( ) { if ! [ -x $(curl) ]; then echo "Curl not installed/not executable" exit 0 fi # get the local tunnel ip local_ip=$(ifconfig tun0 | grep "inet " | cut -d\ -f2) #client_id seems to want random data client_id=$(head -n 100 /dev/urandom | md5 -r | tr -d " -") port=$(curl --silent --data "user=$PIA_USER&pass=$PIA_PASS&client_id=$client_id&local_ip=$local_ip" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment | jq .port) if ! [[ $port =~ ^[0-9]+$ ]]; then echo "Garbled data: $port" exit 0 fi transmission-remote -p $port } is_port_forwarded( ) { # -pt tests for open port. json=$(transmission-remote -pt) if [[ $json == "Port is open: No" ]]; then echo "Closed port detected" get_new_port elif [[ $json == "Port is open: Yes" ]]; then echo "Open port detected" exit 1 fi } check_for_connectivity( ) { if nc -zw 1 google.com 80; then echo "VPN connection up." else echo "VPN connection down. Exiting." exit 0 fi } check_for_connectivity is_port_forwarded exit 1
crontab -e i for insert paste the below */5 * * * * /usr/local/bin/bash /sabnzbd/scripts/port_forward.sh >> /var/log/pia.log 2>&1 then type (esc wq!) This will save it.
Save the file as port_forward.sh
Now create a Cronjob like below.
Code:crontab -e i for insert paste the below */5 * * * * /usr/local/bin/bash /sabnzbd/scripts/port_forward.sh >> /var/log/pia.log 2>&1 then type (esc wq!) This will save it.
root@transmission_1:/ # ./port_forward.sh Transmission Port Forward 2017-01-16-18:17:56 Connection to google.com 80 port [tcp/http] succeeded! VPN connection up. Closed port detected curl: try 'curl --help' or 'curl --manual' for more information cut: bad delimiter curl: no URL specified! curl: try 'curl --help' or 'curl --manual' for more information ./port_forward.sh: line 31: https://www.privateinternetaccess.com/vpninfo/port_forward_assignment: No such file or directory Garbled data: root@transmission_1:/ #