You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

updateTransmissionPort.sh 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #/bin/bash
  2. # Settings
  3. PIA_PASSWD_FILE=/etc/openvpn/credentials.txt
  4. username=$(head -1 $PIA_PASSWD_FILE)
  5. passwd=$(tail -1 $PIA_PASSWD_FILE)
  6. local_vpn_ip=$(ip addr show tun0 | grep inet | awk '{ print $2 }')
  7. pia_client_id_file=/etc/transmission-daemon/pia_client_id
  8. transmission_settings_file=/etc/transmission-daemon/settings.json
  9. port_assignment_url=https://www.privateinternetaccess.com/vpninfo/port_forward_assignment
  10. #
  11. # First get a port from PIA
  12. #
  13. new_client_id() {
  14. head -n 100 /dev/urandom | md5sum | tr -d " -" | tee $pia_client_id_file
  15. }
  16. pia_client_id="$(cat $pia_client_id_file 2>/dev/null)"
  17. if [[ -z "$pia_client_id" ]]; then
  18. echo "Generating new client id for PIA"
  19. pia_client_id=$(new_client_id)
  20. fi
  21. # Get the port
  22. pia_response=$(curl -d "user=$username&pass=$passwd&client_id=$pia_client_id&local_ip=$local_vpn_ip" $port_assignment_url)
  23. new_port=$(echo $pia_response | grep -oE "[0-9]+")
  24. echo "Got new port $new_port from pia"
  25. #
  26. # Now, set port in Transmission
  27. #
  28. # Check if transmission remote is set up with authentication
  29. auth_enabled=$(grep 'rpc-authentication-required\"' $transmission_settings_file | grep -oE 'true|false')
  30. if [[ "true" = "$auth_enabled" ]]
  31. then
  32. echo "transmission auth required"
  33. myauth="--auth username:password"
  34. else
  35. echo "transmission auth not required"
  36. myauth=""
  37. fi
  38. # get current listening port
  39. transmission_peer_port=$(transmission-remote $myauth -si | grep Listenport | grep -oE '[0-9]+')
  40. if [[ "$new_port" != "$transmission_peer_port" ]]
  41. then
  42. transmission-remote $myauth -p "$new_port"
  43. echo "Checking port..."
  44. sleep 10 && transmission-remote $myauth -pt
  45. else
  46. echo "No action needed, port hasn't changed"
  47. fi