Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # PIA VPN Linux port finder
  4. ######################################################
  5. # Configuration Section
  6. ######################################################
  7. # Set the path where this script is stored Ex. "/home/john/"
  8. path_to_script="/home/john/"
  9.  
  10. # Enter you're PIA VPN username and password
  11. piauser="p2099690"
  12. piapass="Hi3kF2g284"
  13.  
  14. # The PIA port request URL (You should not need to change this)
  15. pia_request_url="https://www.privateinternetaccess.com/vpninfo/port_forward_assignment"
  16. ######################################################
  17. # End of Configuration Section
  18. ######################################################
  19.  
  20. # Warning!
  21. # Warning! Do not edit anything below unless to intend to modify this script
  22.  
  23. # Required Dependancies * (likly that all are already installed on most Linux distrobutions)
  24. # =====================
  25. # /bin/bash
  26. # /bin/echo
  27. # /usr/bin/whoami
  28. # /bin/date
  29. # /dev/urandom
  30. # /usr/bin/md5sum
  31. # /usr/bin/tr
  32. # /usr/bin/head
  33. # /sbin/ifconfig
  34. # /bin/grep
  35. # /usr/bin/awk
  36. # /usr/bin/curl
  37.  
  38. clear
  39. echo "========================================================"
  40.  
  41. # Test if running at root or not
  42. user=`whoami`
  43. echo ""
  44. if [ "$user" = "root" ]; then
  45. echo "- User is root (Good!)"
  46. chmod 700 $path_to_script"pia-port.bash" # Set persmissions for the user and root only!
  47. else
  48. echo "This script must be run as root! You are currently logged in as \"$user\""
  49. echo ""
  50. echo "Please switch to root and run the script again!"
  51. echo ""
  52. exit
  53. fi
  54.  
  55. # Gather current time and date
  56. date_now=$(/bin/date +"%Y-%m-%d_%H:%M:%S")
  57.  
  58. # Define path to pia_client_id
  59. pia_client_id=$path_to_script"pia_client_id"
  60. #echo "path for pia_client_id = "$pia_client_id # Uncomment for debugging
  61. #echo "" # Uncomment for debugging
  62.  
  63. # Check for pia_client_id file
  64. if [ -f $pia_client_id ]; then
  65. echo "- pia_client_id file exists (Good!)"
  66. else
  67. echo "pia_client_id file does not exist!"
  68. echo ""
  69. echo "Generateing pia_client_id file now."
  70. head -n 100 /dev/urandom | md5sum | tr -d " -" > $pia_client_id
  71. chmod 600 $pia_client_id # Set persmissions for the user and root only!
  72. echo ""
  73. fi
  74.  
  75. # Check for tun0
  76. tuncheck=$( { /sbin/ifconfig tun0; } 2>&1 )
  77. #echo "tun check = "$tuncheck # Uncomment for debugging
  78. #echo "" # Uncomment for debugging
  79.  
  80. # Display info about tunnel
  81. #tunnelinfo=$( { /sbin/ip link show dev tun0; } 2>&1 ) # Uncomment for debugging
  82. #echo "$tunnelinfo" # Uncomment for debugging
  83. #echo "" # Uncomment for debugging
  84.  
  85. tunnotfound="not found"
  86. if [ "${tuncheck/$tunnotfound}" = "$tuncheck" ] ; then
  87. echo "- VPN tunnel appears to be up and connected (Good!)"
  88. else
  89. echo "Error detected! tun0 was \"not found\""
  90. echo "Please make sure both internet and the VPN is connected!"
  91. echo ""
  92. echo "Then run this script again!"
  93. echo ""
  94. exit
  95. fi
  96.  
  97. if [ $? -eq 0 ]; then
  98. # Working on debian based bl-Hydrogen-rc1
  99. local_ip=$(/sbin/ifconfig tun0 | /bin/grep inet | /usr/bin/awk -F: '{print $2}' | /usr/bin/awk '{print $1}')
  100.  
  101. if [ "$local_ip" = "" ]; then
  102. # The following worked for Manjaro
  103. echo "- local_ip pull failed! Attempting alternet pull!"
  104. local_ip=$(/sbin/ifconfig tun0 | /bin/grep inet | awk '{print $2 }')
  105.  
  106. fi
  107. else
  108. # Inform user that the tunnel is not up! You are not connected to the VPN!
  109. echo "tun0 not present - Please connect to the VPN and run this script again" >> $path_to_script"openvpn_pia_port_fwd.txt"
  110. fi
  111. # Pull client_id from path and file for var
  112. client_id=$(cat $path_to_script"pia_client_id")
  113.  
  114. # Var output for reference
  115. echo ""
  116. echo "Requesting a port with the following information from..."
  117. echo "... $pia_request_url"
  118. echo "- Your PIA username and password (Example p1234567)"
  119. echo "- client_id = $client_id"
  120. echo "- local_ip = $local_ip"
  121. echo ""
  122. echo "Hint: Delete pia_client_id to cycle to a new port number with..."
  123. echo "rm pia_client_id & ./pia-port.bash"
  124. echo ""
  125. echo "One moment..."
  126. echo ""
  127.  
  128. # The magic part! PIA don't let me down!
  129. # Post a request for a port to PIA's https://www.privateinternetaccess.com/vpninfo/port_forward_assignment
  130. pia_response=$(/usr/bin/curl -d "user=$piauser&pass=$piapass&client_id=$client_id&local_ip=$local_ip" $pia_request_url 2>/dev/null)
  131.  
  132. # Check if PIA actually responded or not
  133. if [ ${#pia_response} -gt 0 ]; then
  134. echo "PIA has responded as of $date_now"
  135.  
  136. # Remove this part or fix ?
  137. # Code that might work for /usr/bin/transmission-remote
  138. # Seperate only the port number from PIA's resonce with grep and put into var
  139. #transmission_port=`echo $pia_response | /bin/grep -oE '[0-9]+'`
  140. # If the response is within the range (link with transmission torrent app)
  141. # if [ $transmission_port -gt 1024 ] && [ $transmission_port -lt 65535 ]; then
  142. # echo "transmission_port: $transmission_port" >> /root/test-scripts/openvpn_pia_port_fwd.txt
  143. # sudo /usr/bin/transmission-remote -p $transmission_port | tee -a /root/test-scripts/openvpn_pia_port_fwd.txt
  144. # else
  145. # echo "transmission_port: error" >> /root/test-scripts/openvpn_pia_port_fwd.txt
  146. # fi
  147. else
  148. # Inform user that the pia server failed to respond in an expected manner
  149. echo "Failed to get response from PIA with request sent."
  150. echo ""
  151. exit
  152. fi
  153.  
  154. echo "PIA's Response: $pia_response"
  155. echo ""
  156. echo "========================================================"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement