Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # PIA VPN Linux port finder
- ######################################################
- # Configuration Section
- ######################################################
- # Set the path where this script is stored Ex. "/home/john/"
- path_to_script="/home/john/"
- # Enter you're PIA VPN username and password
- piauser="p2099690"
- piapass="Hi3kF2g284"
- # The PIA port request URL (You should not need to change this)
- pia_request_url="https://www.privateinternetaccess.com/vpninfo/port_forward_assignment"
- ######################################################
- # End of Configuration Section
- ######################################################
- # Warning!
- # Warning! Do not edit anything below unless to intend to modify this script
- # Required Dependancies * (likly that all are already installed on most Linux distrobutions)
- # =====================
- # /bin/bash
- # /bin/echo
- # /usr/bin/whoami
- # /bin/date
- # /dev/urandom
- # /usr/bin/md5sum
- # /usr/bin/tr
- # /usr/bin/head
- # /sbin/ifconfig
- # /bin/grep
- # /usr/bin/awk
- # /usr/bin/curl
- clear
- echo "========================================================"
- # Test if running at root or not
- user=`whoami`
- echo ""
- if [ "$user" = "root" ]; then
- echo "- User is root (Good!)"
- chmod 700 $path_to_script"pia-port.bash" # Set persmissions for the user and root only!
- else
- echo "This script must be run as root! You are currently logged in as \"$user\""
- echo ""
- echo "Please switch to root and run the script again!"
- echo ""
- exit
- fi
- # Gather current time and date
- date_now=$(/bin/date +"%Y-%m-%d_%H:%M:%S")
- # Define path to pia_client_id
- pia_client_id=$path_to_script"pia_client_id"
- #echo "path for pia_client_id = "$pia_client_id # Uncomment for debugging
- #echo "" # Uncomment for debugging
- # Check for pia_client_id file
- if [ -f $pia_client_id ]; then
- echo "- pia_client_id file exists (Good!)"
- else
- echo "pia_client_id file does not exist!"
- echo ""
- echo "Generateing pia_client_id file now."
- head -n 100 /dev/urandom | md5sum | tr -d " -" > $pia_client_id
- chmod 600 $pia_client_id # Set persmissions for the user and root only!
- echo ""
- fi
- # Check for tun0
- tuncheck=$( { /sbin/ifconfig tun0; } 2>&1 )
- #echo "tun check = "$tuncheck # Uncomment for debugging
- #echo "" # Uncomment for debugging
- # Display info about tunnel
- #tunnelinfo=$( { /sbin/ip link show dev tun0; } 2>&1 ) # Uncomment for debugging
- #echo "$tunnelinfo" # Uncomment for debugging
- #echo "" # Uncomment for debugging
- tunnotfound="not found"
- if [ "${tuncheck/$tunnotfound}" = "$tuncheck" ] ; then
- echo "- VPN tunnel appears to be up and connected (Good!)"
- else
- echo "Error detected! tun0 was \"not found\""
- echo "Please make sure both internet and the VPN is connected!"
- echo ""
- echo "Then run this script again!"
- echo ""
- exit
- fi
- if [ $? -eq 0 ]; then
- # Working on debian based bl-Hydrogen-rc1
- local_ip=$(/sbin/ifconfig tun0 | /bin/grep inet | /usr/bin/awk -F: '{print $2}' | /usr/bin/awk '{print $1}')
- if [ "$local_ip" = "" ]; then
- # The following worked for Manjaro
- echo "- local_ip pull failed! Attempting alternet pull!"
- local_ip=$(/sbin/ifconfig tun0 | /bin/grep inet | awk '{print $2 }')
- fi
- else
- # Inform user that the tunnel is not up! You are not connected to the VPN!
- echo "tun0 not present - Please connect to the VPN and run this script again" >> $path_to_script"openvpn_pia_port_fwd.txt"
- fi
- # Pull client_id from path and file for var
- client_id=$(cat $path_to_script"pia_client_id")
- # Var output for reference
- echo ""
- echo "Requesting a port with the following information from..."
- echo "... $pia_request_url"
- echo "- Your PIA username and password (Example p1234567)"
- echo "- client_id = $client_id"
- echo "- local_ip = $local_ip"
- echo ""
- echo "Hint: Delete pia_client_id to cycle to a new port number with..."
- echo "rm pia_client_id & ./pia-port.bash"
- echo ""
- echo "One moment..."
- echo ""
- # The magic part! PIA don't let me down!
- # Post a request for a port to PIA's https://www.privateinternetaccess.com/vpninfo/port_forward_assignment
- pia_response=$(/usr/bin/curl -d "user=$piauser&pass=$piapass&client_id=$client_id&local_ip=$local_ip" $pia_request_url 2>/dev/null)
- # Check if PIA actually responded or not
- if [ ${#pia_response} -gt 0 ]; then
- echo "PIA has responded as of $date_now"
- # Remove this part or fix ?
- # Code that might work for /usr/bin/transmission-remote
- # Seperate only the port number from PIA's resonce with grep and put into var
- #transmission_port=`echo $pia_response | /bin/grep -oE '[0-9]+'`
- # If the response is within the range (link with transmission torrent app)
- # if [ $transmission_port -gt 1024 ] && [ $transmission_port -lt 65535 ]; then
- # echo "transmission_port: $transmission_port" >> /root/test-scripts/openvpn_pia_port_fwd.txt
- # sudo /usr/bin/transmission-remote -p $transmission_port | tee -a /root/test-scripts/openvpn_pia_port_fwd.txt
- # else
- # echo "transmission_port: error" >> /root/test-scripts/openvpn_pia_port_fwd.txt
- # fi
- else
- # Inform user that the pia server failed to respond in an expected manner
- echo "Failed to get response from PIA with request sent."
- echo ""
- exit
- fi
- echo "PIA's Response: $pia_response"
- echo ""
- echo "========================================================"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement