Skip to content

Instantly share code, notes, and snippets.

@mgithens1
Last active March 15, 2024 08:03
Show Gist options
  • Save mgithens1/108afa603e0f4598c7e0b0ad94175fcf to your computer and use it in GitHub Desktop.
Save mgithens1/108afa603e0f4598c7e0b0ad94175fcf to your computer and use it in GitHub Desktop.
Bash script for updating Transmission IP/port for Private Internet Access via OpenVPN.
#!/bin/sh
# FILL OUT YOUR INFO HERE
LOGIN=PIA LOGIN HERE
PASSWORD=PIA PASSWORD HERE
USER=TRANSMISSION LOGIN HERE
PASS=TRANSMISSION PASSWORD HERE
BIN="/usr/bin/transmission-remote"
LOGGING="/home/USERNAME/transmission_logging.txt"
# ASSUMPTIONS
# VPN adapter is "tun0", so update if you need to
# No changes needed below here
# Check for torrents, if 2 then stop Open VPN and quit script
TORRENTCOUNT=`$BIN -n $USER:$PASS --list | wc -l`
OPENVPNRUNNING=`ip link show dev tun0 | grep 1500 | wc -l`
if [ $TORRENTCOUNT -le 2 ];
then
if [ $OPENVPNRUNNING = 1 ];
then
service openvpn stop
echo `date +"%m-%d-%Y %T"`" - OpenVPN Stopped" >> $LOGGING
exit
fi
else
if [ $OPENVPNRUNNING = 0 ];
then
service openvpn start
echo `date +"%m-%d-%Y %T"`" - OpenVPN Started" >> $LOGGING
sleep 10
fi
# Update Bind address
VPNADDR=`ifconfig tun0 | grep -oE "inet addr: *10\.[0-9]+\.[0-9]+\.[0-9]+"|tr -d "a-z :"|tee /tmp/vpn_ip`
CURRENTBIND=`cat /etc/transmission-daemon/settings.json | grep bind-address-ipv4 | cut -f2 -d":" | tr -d " \"\,"`
if [ "$CURRENTBIND" != "$VPNADDR" ];then
# Transmission will not all updating of bind address while running.
service transmission-daemon stop
sed -i "s/.*bind-address-ipv4.*/ \"bind-address-ipv4\"\: \"$VPNADDR\",/g" /etc/transmission-daemon/settings.json
service transmission-daemon start
echo `date +"%m-%d-%Y %T"`" - Bind Address updated to "$VPNADDR >> $LOGGING
sleep 10
fi
# PORT FORWARD UPDATE
PORTOPEN=`$BIN -n $USER:$PASS --port-test | cut -f2 -d":" | sed 's/ //g'`
if [ $PORTOPEN != Yes ];then
CURRENTIP=`ifconfig tun0 | grep -oE "inet addr: *10\.[0-9]+\.[0-9]+\.[0-9]+"|tr -d "a-z :"|tee /tmp/vpn_ip`
PORTFOUND=`curl -s -d "user=$LOGIN&pass=$PASSWORD&client_id=$(cat ~/.pia_client_id)&local_ip=$CURRENTIP" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment | sed 's/[^0-9]*//g'`
echo `date +"%m-%d-%Y %T"`" - VPN Port Updated to "$PORTFOUND >> $LOGGING
$BIN -n $USER:$PASS --port $PORTFOUND
fi
# Clean up completed torrents, one per run
if [ $TORRENTCOUNT -gt 0 ];then
TORRENTID=`$BIN -n $USER:$PASS --list | sed -n 2p | awk '{ print $1 }'`
TORRENTSTATE=`$BIN -n $USER:$PASS -t $TORRENTID --info | grep State | cut -d' ' -f4`
TORRENTNAME=`$BIN -n $USER:$PASS -t $TORRENTID --info | grep Name | cut -d':' -f2 | sed 's/^ //g'`
if [ $TORRENTSTATE = "Finished" ];then
$BIN -n $USER:$PASS -t $TORRENTID --remove
echo `date +"%m-%d-%Y %T"`" - Torrent Removed - $TORRENTNAME" >> $LOGGING
fi
fi
fi
@mgithens1
Copy link
Author

mgithens1 commented Jan 21, 2018

REQUIREMENTS:

  • Ubuntu Server 16.04 LTS
  • Transmission Client
  • OpenVPN configured to connect to Private Internet Access

PURPOSE:

I've been running a version of this in a cron job for 18months, without fail. PIA is great, but you will lose connection every once in a while, so you'll pull a new IP. Also, if you are maximizing your connection you have port forwarding enabled thru PIA, but this changes every so often also.

This script will reset the IP and the Port as needed.

SUGGESTED USE:

Put this in a cron job and have it run every 5 mins to keep your torrents flowing.

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