GUIDE: Setting up Transmission with OpenVPN and PIA

thijsjek

Dabbler
Joined
Aug 12, 2017
Messages
19
I had pia/openvpn running on my transmission jail (freenas 11) and everything worked correctly. Later i noticed that transmission was wonky and did a reinstall of the plugin.
I installed openvpn like in the start thread by myself, and now: service openvpn start, doens't start actually.
tail /var/log/messages
Code:
Sep 10 13:13:51 transmission_1 openvpn[76796]: TCP/UDP: Preserving recently used remote address: [AF_INET]46.166.186.234:1198	 
Sep 10 13:13:51 transmission_1 openvpn[76796]: UDP link local: (not bound)														
Sep 10 13:13:51 transmission_1 openvpn[76796]: UDP link remote: [AF_INET]46.166.186.234:1198										
Sep 10 13:13:51 transmission_1 openvpn[76796]: WARNING: this configuration may cache passwords in memory -- use the auth-nocache opt
ion to prevent this																												
Sep 10 13:13:51 transmission_1 openvpn[76796]: [296a267602ebc4b8d1ec2f525b433900] Peer Connection Initiated with [AF_INET]46.166.186
.234:1198																														 
Sep 10 13:13:57 transmission_1 openvpn[76796]: TUN/TAP device /dev/tun0 opened													
Sep 10 13:13:57 transmission_1 openvpn[76796]: do_ifconfig, tt->did_ifconfig_ipv6_setup=0										 
Sep 10 13:13:57 transmission_1 openvpn[76796]: /sbin/ifconfig tun0 10.61.10.6 10.61.10.5 mtu 1500 netmask 255.255.255.255 up		
Sep 10 13:13:57 transmission_1 openvpn[76796]: FreeBSD ifconfig failed: external program exited with error status: 1				
Sep 10 13:13:57 transmission_1 openvpn[76796]: Exiting due to fatal error

Code:
ifconfig -a
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384																  
		options=600003<RXCSUM,TXCSUM,RXCSUM_IPV6,TXCSUM_IPV6>																	  
		inet6 ::1 prefixlen 128																									
		inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1																				
		inet 127.0.0.1 netmask 0xff000000																						  
		nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>																				  
		groups: lo																												
epair5b: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500													  
		options=8<VLAN_MTU>																										
		ether 72:28:d2:03:a3:ed																									
		inet 192.168.178.44 netmask 0xffffff00 broadcast 192.168.178.255															
		nd6 options=9<PERFORMNUD,IFDISABLED>																						
		media: Ethernet 10Gbase-T (10Gbase-T <full-duplex>)																		
		status: active																											
		groups: epair  

How can i solve this?

EDIT: After a reboot of the server, not just jail, all problems are solved
 
Last edited:

indivision

Guru
Joined
Jan 4, 2013
Messages
806
When there is connection instability, transmission will switch off of the VPN connection and start using the server IP. The potential for that to happen at unpredictable times pretty much defeats the purpose of using VPN and may be giving people a false sense of security. There should at least be a disclaimer in the OP about this.

That said, is there a guide or second script for blocking all connections except through the VPN? I have found a few guides in other posts. But, they didn't work. They appear to be either outdated or meant for different variations of installing openvpn (different filenames, folders, etc.).
 

Amsoil_Jim

Contributor
Joined
Feb 22, 2016
Messages
175
is anyone have any issues with the port-forward script? I keep getting and unauthorized user error.
Code:
Loading port forward assignment information..
{"port":32579}
Unexpected response: <h1>401: Unauthorized</h1>Unauthorized User
 

lkh5650

Cadet
Joined
Sep 8, 2017
Messages
4
So my transmission was working well with PIA and now my listening ports are closed even with the enable port forwarding checked on transmission. I found this "https://forums.freenas.org/index.ph...rt-closed-on-transmission-with-pia-vpn.27628/" and that person claimed that your script solved the problem!! I am however not sure on how/what to do with your script. Am I supposed to make a new file? or add this in current openvpn.conf file..? little bit of direction would be really appreciated!

I am interested in using the port forward feature of PIA. I hacked together this script starting from the PIA script, and it seems to work (forwards port to Transmission). I am still learning FreeNAS and scripting so if anyone has a more elegant solution I would be interested.

(this runs inside the jail with OpenVPN and Transmission)

Code:
#! /usr/local/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
	local_ip=`ifconfig tun0|grep -oE "inet addr: *10\.[0-9]+\.[0-9]+\.[0-9]+"|tr -d "a-z :"|tee /tmp/vpn_ip`
	client_id=`head -n 100 /dev/urandom | md5sum | tr -d " -"`
  fi
  if [ "$(uname)" == "FreeBSD" ]; 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 --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`
  echo $json
  PORTNUM=`echo $json | grep -oE "[0-9]+"`
}

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

transmission-remote -p $PORTNUM

exit 0
 

Baenwort

Explorer
Joined
Feb 19, 2015
Messages
93
When there is connection instability, transmission will switch off of the VPN connection and start using the server IP. The potential for that to happen at unpredictable times pretty much defeats the purpose of using VPN and may be giving people a false sense of security. There should at least be a disclaimer in the OP about this.

That said, is there a guide or second script for blocking all connections except through the VPN? I have found a few guides in other posts. But, they didn't work. They appear to be either outdated or meant for different variations of installing openvpn (different filenames, folders, etc.).

If you haven't found this yet there is a guide to setting up a monitoring script that shuts down the jail's internet connection when the PIA openVPN connection fails. https://forums.freenas.org/index.ph...ith-openvpn-and-pia.24566/page-20#post-341622 I don't know for certain if it will work but it reads like it should.
 

lkh5650

Cadet
Joined
Sep 8, 2017
Messages
4

lkh5650

Cadet
Joined
Sep 8, 2017
Messages
4
after many many hours of trials and errors, i got the port forwarding to work but only manually when i do
./port_forward.sh

is there anyway that i can have the port_forward.sh to be performed when transmission starts?
 

Rikkard

Dabbler
Joined
Dec 2, 2014
Messages
26
When there is connection instability, transmission will switch off of the VPN connection and start using the server IP. The potential for that to happen at unpredictable times pretty much defeats the purpose of using VPN and may be giving people a false sense of security. There should at least be a disclaimer in the OP about this.

That said, is there a guide or second script for blocking all connections except through the VPN? I have found a few guides in other posts. But, they didn't work. They appear to be either outdated or meant for different variations of installing openvpn (different filenames, folders, etc.).

I use ipfw. Then a configfile, mine look:


add 01000 allow log udp from 192.168.1.0/24 to 8.8.8.8 dst-port 53 keep-state

add 01008 allow ip from 192.168.1.0/24 to 192.168.1.0/24 keep-state


add 02000 allow ip from 192.168.1.0/24 to 185.213.152.131 keep-state
add 02002 allow ip from 192.168.1.0/24 to 185.213.152.132 keep-state
add 02004 allow ip from 192.168.1.0/24 to 185.213.152.134 keep-state


add 04000 allow ip from 127.0.0.1 to any
add 05004 allow ip from 10.15.0.0/16 to any
add 05006 allow ip from any to 10.15.0.0/16
add 65534 deny ip from any to any
 

Michael Sparks

Explorer
Joined
Apr 23, 2014
Messages
56
Any idea why the script is no longer working?

Code:
jls
jexec [JAILID] tcsh
cd /tmp
wget --quiet --no-check-certificate -O pia.sh https://gist.githubusercontent.com/jed-frey/6d475dcc34c710f62a7c/raw/fcf18484225b0676fe0c556e7798cc08cdc7a631/pia.sh
chmod +x pia.sh
./pia.sh


Code:
Starting openvpn.
Waiting 10 seconds for OpenVPN to spin up
If these are different, OpenVPN is working
Old IP: 72.234.183.208
New IP: 72.234.183.208


Not a big issue was able to complete using update cli in first post. Thanks
 
Last edited:

cloud

Dabbler
Joined
Mar 2, 2014
Messages
10
I have both ipfw and openvpn running and they appear to be working correctly

I'm getting a 403 forbidden error from the WEB GUI when I try to access it from my LAN.
Unauthorized IP Address.
Either disable the IP address whitelist or add your address to it.
If you're editing settings.json, see the 'rpc-whitelist' and 'rpc-whitelist-enabled' entries.
If you're still using ACLs, use a whitelist instead. See the transmission-daemon manpage for details.


I'm using the same ipfw rules as listed, even when I turn off the firewall and flush the rules.
I wasn't using the RPC whitelist before so nothing should have changed.

How/Where can I allow or whitelist my LAN network?
 

goerz

Dabbler
Joined
Dec 19, 2011
Messages
17
I successfully installed openvpn on my transmission jail on FreeNAS-9.10.2-U6. I also set up the internal firewall so that transmission does not connect if the vpn is down. I read that PIA has changed the port forwarding procedure, so the port forwarding scripts posted in this thread don't work anymore (https://www.privateinternetaccess.com/forum/discussion/23431/new-pia-port-forwarding-api).
Although I am no computer expert, I managed to make this script to work. It does not need your PIA username and password, but it needs your transmission credentials.

Code:
#!/usr/bin/env bash
#
# Enable port forwarding when using Private Internet Access
# install curl first (pkg install curl)
# Usage:
#  ./port_forwarding.sh

TRANSUSER=your_transmission_username
TRANSPASS=your_transmission_password
TRANSHOST=localhost

error( )
{
  echo "$@" 1>&2
  exit 1
}

error_and_usage( )
{
  echo "$@" 1>&2
  usage_and_exit 1
}

usage( )
{
  echo "Usage: `dirname $0`/$PROGRAM"
}

usage_and_exit( )
{
  usage
  exit $1
}

version( )
{
  echo "$PROGRAM version $VERSION"
}


port_forward_assignment( )
{
  echo 'Loading port forward assignment information...'
  if [ "$(uname)" == "Linux" ]; then
	client_id=`head -n 100 /dev/urandom | sha256sum | tr -d " -"`
  fi
  if [ "$(uname)" == "FreeBSD" ]; then
	   client_id=`head -n 100 /dev/urandom | shasum -a 256 | tr -d " -"`
  fi

  json=`curl "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null`
  if [ "$json" == "" ]; then
	echo Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding
	exit 0
  fi

  echo server returned $json
 
   #trim VPN forwarded port from JSON
	PORT=$(echo $json | awk 'BEGIN{r=1;FS="[{}\":]+"} /port/{r=0; print $3} END{exit r}')
	echo if successful, trimmed port is:$PORT
	
	#change transmission port on the fly
	
	transmission-remote $TRANSHOST --auth $TRANSUSER:$TRANSPASS -p "$PORT"
	 echo remember to run no longer than 2 mins after reconnecting/connecting to vpn server.
}



EXITCODE=0
PROGRAM=`basename $0`
VERSION=2.1

while test $# -gt 0
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



However, this script must run within 2 minutes of the vpn connection, but openvpn should have time to connect before this script runs. So I would like this script to run 1 minute after openvpn connects at startup. How can I automate this task when I start the jail? What if the vpn disconnects and then it reconnects?
Thanks,
goerz
 
Last edited:

ChaosBlades

Contributor
Joined
Jul 4, 2015
Messages
137
I successfully installed openvpn on my transmission jail on FreeNAS-9.10.2-U6. I also set up the internal firewall so that transmission does not connect if the vpn is down. I read that PIA has changed the port forwarding procedure, so the port forwarding scripts posted in this thread don't work anymore (https://www.privateinternetaccess.com/forum/discussion/23431/new-pia-port-forwarding-api).
Although I am no computer expert, I managed to make this script to work. It does not need your PIA username and password, but it needs your transmission credentials.

Code:
#!/usr/bin/env bash
#
# Enable port forwarding when using Private Internet Access
# install curl first (pkg install curl)
# Usage:
#  ./port_forwarding.sh

TRANSUSER=your_transmission_username
TRANSPASS=your_transmission_password
TRANSHOST=localhost

error( )
{
  echo "$@" 1>&2
  exit 1
}

error_and_usage( )
{
  echo "$@" 1>&2
  usage_and_exit 1
}

usage( )
{
  echo "Usage: `dirname $0`/$PROGRAM"
}

usage_and_exit( )
{
  usage
  exit $1
}

version( )
{
  echo "$PROGRAM version $VERSION"
}


port_forward_assignment( )
{
  echo 'Loading port forward assignment information...'
  if [ "$(uname)" == "Linux" ]; then
	client_id=`head -n 100 /dev/urandom | sha256sum | tr -d " -"`
  fi
  if [ "$(uname)" == "FreeBSD" ]; then
	   client_id=`head -n 100 /dev/urandom | shasum -a 256 | tr -d " -"`
  fi

  json=`curl "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null`
  if [ "$json" == "" ]; then
	echo Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding
	exit 0
  fi

  echo server returned $json
 
   #trim VPN forwarded port from JSON
	PORT=$(echo $json | awk 'BEGIN{r=1;FS="[{}\":]+"} /port/{r=0; print $3} END{exit r}')
	echo if successful, trimmed port is:$PORT
	
	#change transmission port on the fly
	
	transmission-remote $TRANSHOST --auth $TRANSUSER:$TRANSPASS -p "$PORT"
	 echo remember to run no longer than 2 mins after reconnecting/connecting to vpn server.
}



EXITCODE=0
PROGRAM=`basename $0`
VERSION=2.1

while test $# -gt 0
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



However, this script must run within 2 minutes of the vpn connection, but openvpn should have time to connect before this script runs. So I would like this script to run 1 minute after openvpn connects at startup. How can I automate this task when I start the jail? What if the vpn disconnects and then it reconnects?
Thanks,
goerz
Reminder, port forwarding is only available on the following gateways or you will get the error "Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding".
  • CA Toronto
  • CA Montreal
  • CA Vancouver
  • Netherlands
  • Switzerland
  • Sweden
  • France
  • Germany
  • Romania
  • Israel
 

sam-wade

Cadet
Joined
Sep 14, 2017
Messages
1
Hi Guys,
Just tried setting this up, everything works fine till the end of the first section, where I check my IP address. When I check it it displays back with my external IP address not with the vpn server one. Any help would be appreciated
 

dobbies

Cadet
Joined
Mar 2, 2017
Messages
3
Hi jafrey

I just wanted to thank you for your now "quite ancient" post. I'm far from an expert on the FreeNAS system, but I've been trying to set up a PIA VPN in the transmission jail for days now without success. Most of the posts I've found on the forum have left me with nothing less than confusion. I don't know how I found your post but within an hour of reading it I had a PIA VPN up and running on my server. Thank You.
 
Last edited by a moderator:

dobbies

Cadet
Joined
Mar 2, 2017
Messages
3
Hi Guys,
Just tried setting this up, everything works fine till the end of the first section, where I check my IP address. When I check it it displays back with my external IP address not with the vpn server one. Any help would be appreciated

Make sure you are checking your external IP from within your jail.

Example: root@transmission_??:/ # wget -qO - http://wtfismyip.com/text

If you use the same URL from your root you should see your normal IP.

Hope this helps?
 
Last edited by a moderator:

Simplicity

Dabbler
Joined
Jun 13, 2015
Messages
28
I followed your guide and everything works. I failed at the Reddit steps #3/4 and I was wondering once I am setup how do you switch PIA servers? Can you write in your own words how to do the step 3/4 and changing the PIA server once configured?
 

denist

Contributor
Joined
Jan 28, 2013
Messages
188
Hi Guys,

Just update my port_forward.sh script with the new API Port Forwarding from PIA.

Code:
#!/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
#

# 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( )
{
  # Check if curl is installed
  if ! [ -x /usr/local/bin/curl ]; then
   echo "Curl not installed/not executable"
   exit 0
  fi
  # dynamically figure out the tunnel adapter name
  tunnel_adapter=`ifconfig | grep "tun" | cut -d ":" -f1`
  local_ip=$(ifconfig $tunnel_adapter | grep "inet " | cut -d\  -f2)
   
  #client_id seems to want sha256sum data
  client_id_file="/usr/local/etc/openvpn/pia_client_id"
  if [ ! -f "$client_id_file" ]; then
   if hash shasum 2>/dev/null; then
	   head -n 100 /dev/urandom | shasum -a 256 | tr -d " -" > "$client_id_file"
  elif hash sha256sum 2>/dev/null; then
	   head -n 100 /dev/urandom | sha256sum | tr -d " -" > "$client_id_file"
   else
	   echo "Please install shasum or sha256sum, and make sure it is visible in your \$PATH"
	   exit 1
   fi   
  fi
  client_id=`cat "$client_id_file"`
  json=$(curl --interface $tunnel_adapter "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null)
  if [ "$json" == "" ]; then
	json='Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding'
  fi

  #trim VPN forwarded port from JSON
  port=$(echo $json | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
 
  # test to make sure that the port is actually a number
  if ! [[ $port =~ ^[0-9]+$ ]]; then
   echo $json
   exit 1
  fi
 
  transmission-remote -p $port
 
   echo $json
   
}

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"
	exit 1

  fi
}

check_for_connectivity
is_port_forwarded

exit 1


The i created the cronjob to run every 2 mins as follows.

Code:
*/2 * * * * /usr/local/bin/bash /sabnzbd/scripts/port_forward.sh >> /var/log/pia.log 2>&1


Since i am using the Kill Script there is no internet when the Server boots up. Openvpn starts but the ipfw rules are blocking internet access until the Kill script runs. I have this set on 5 min intervals and the port forward on 2 min intervals. This gives it enough time for the openvpn to start and ipfw to write the correct rules, then finally the port_forward script runs and creates the port forward in transmission. You do not require any usernames in my script which has been mashed up between a few scripts that i have found. Hope this helps someone else.
 

denist

Contributor
Joined
Jan 28, 2013
Messages
188
Final Port script that work. Above there was an error.

Code:
#!/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
#

# 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( )
{

   echo 'Loading port forward assignment information...'

  # Check if curl is installed
  if ! [ -x /usr/local/bin/curl ]; then
   echo "Curl not installed/not executable"
   exit 1
  fi
  # dynamically figure out the tunnel adapter name
  tunnel_adapter=`ifconfig | grep "tun" | cut -d ":" -f1`
  local_ip=$(ifconfig $tunnel_adapter | grep "inet " | cut -d\  -f2)
   
  # client_id seems to want shasum/sha256sum data
  client_id_file="/usr/local/etc/openvpn/pia_client_id"
  if [ ! -f "$client_id_file" ]; then
   if hash shasum 2>/dev/null; then
	   head -n 100 /dev/urandom | shasum -a 256 | tr -d " -" > "$client_id_file"
  elif hash sha256sum 2>/dev/null; then
	   head -n 100 /dev/urandom | sha256sum | tr -d " -" > "$client_id_file"
   else
	   echo "Please install shasum or sha256sum, and make sure it is visible in your \$PATH"
	   exit 1
   fi
  fi
 
  # port_forward_assignment
  client_id=`cat "$client_id_file"`
  json=$(curl --interface $tunnel_adapter "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null)
  if [ "$json" == "" ]; then
	json='Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding'
  fi

  # trim VPN forwarded port from JSON
  port=$(echo $json | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
 
  # test to make sure that the port is actually a number
  if ! [[ $port =~ ^[0-9]+$ ]]; then
   echo $json
   exit 1
  fi
 
  transmission-remote -p $port
 
   echo 'Port forward successful'
   echo $local_ip:$port
   exit 1
   
}

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"
  is_port_forwarded
  else
  echo "VPN connection down"
	exit 1

  fi
}

check_for_connectivity
is_port_forwarded

exit 1
 
Last edited:

thijsjek

Dabbler
Joined
Aug 12, 2017
Messages
19
Final Port script that work. Above there was an error.

Code:
#!/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
#

# 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( )
{

   echo 'Loading port forward assignment information...'

  # Check if curl is installed
  if ! [ -x /usr/local/bin/curl ]; then
   echo "Curl not installed/not executable"
   exit 1
  fi
  # dynamically figure out the tunnel adapter name
  tunnel_adapter=`ifconfig | grep "tun" | cut -d ":" -f1`
  local_ip=$(ifconfig $tunnel_adapter | grep "inet " | cut -d\  -f2)
 
  # client_id seems to want shasum/sha256sum data
  client_id_file="/usr/local/etc/openvpn/pia_client_id"
  if [ ! -f "$client_id_file" ]; then
   if hash shasum 2>/dev/null; then
	   head -n 100 /dev/urandom | shasum -a 256 | tr -d " -" > "$client_id_file"
  elif hash sha256sum 2>/dev/null; then
	   head -n 100 /dev/urandom | sha256sum | tr -d " -" > "$client_id_file"
   else
	   echo "Please install shasum or sha256sum, and make sure it is visible in your \$PATH"
	   exit 1
   fi
  fi
 
  # port_forward_assignment
  client_id=`cat "$client_id_file"`
  json=$(curl --interface $tunnel_adapter "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null)
  if [ "$json" == "" ]; then
	json='Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding'
  fi

  # trim VPN forwarded port from JSON
  port=$(echo $json | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
 
  # test to make sure that the port is actually a number
  if ! [[ $port =~ ^[0-9]+$ ]]; then
   echo $json
   exit 1
  fi
 
  transmission-remote -p $port
 
   echo 'Port forward successful'
   echo $local_ip:$port
   exit 1
 
}

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"
  is_port_forwarded
  else
  echo "VPN connection down"
	exit 1

  fi
}

check_for_connectivity
is_port_forwarded

exit 1

How can i put in the transmission password and username to authenticate with transmission?
 

denist

Contributor
Joined
Jan 28, 2013
Messages
188
transmission-remote -n 'transmission:transmission' -p $port


Sent from my iPhone using Tapatalk
 
Top