Open In App

How to Automate VPN to change IP location on Ubuntu using Python?

Last Updated : 04 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

To Protect Our system from unauthorized users Access you can spoof our system’s IP Address using VPN service provided by different organizations. You can set up a VPN on your system for free.  

After you set up and log in to the VPN over the Ubuntu system you need to manually connect with different VPN servers after some duration. We can automate it using python so that automatically the IP address of our system keeps changing after some duration so that no one can have track of our system anyhow.  It will make our system more protected.

Follow the steps to Automate VPN using Python:

Step 1: Open your terminal (Ctrl+Alt+T) and create a file using gedit by typing the following command on the terminal.

gedit gfg.py

Step 2: import the modules of python into the opened file.

Python3




# import required modules
import os
from time import sleep
import random


 

 

Step 3: Create a list of Free VPN server codes provided by Windscribe (VPN).

 

Python3




# list of VPN server codes
codeList = ["TR", "US-C", "US", "US-W", "CA", "CA-W",
            "FR", "DE", "NL", "NO", "RO", "CH", "GB", "HK"]


 

 

Step 4: Start a try block connect with Windscribe using os module

 

os.system("windscribe connect")

 

And, then start an infinite loop and write some lines under it.

 

  • Choose a random code from codelist using random module.
choiceCode = random.choice(codeList)
  • Create a random sleep for 15 to 20 min after which the IP of the system gets changed using time and random modules.
sleep(random.randrange(120,300))
  • Connect with the randomly chosen VPN code.
os.system("windscribe connect "+ choiceCode)

 

Python3




try:
 
    # connect to VPN
    os.system("windscribe connect")
    while True:
 
        # assigning a random VPN server code
        choiceCode = random.choice(codeList)
 
        # changing IP after a particular time period
        sleep(random.randrange(120, 300))
 
        # connecting to a different VPN server
        print("!!! Changing the IP Address........")
        os.system("windscribe connect " + choiceCode)


Step 5: Start a catch block and then:

  • Disconnect the VPN, it will run if it gets any error.
os.system("windscribe disconnect")
  • Display a disconnection message here.
print("sorry, some error has occurred..!!")

Python3




except:
 
    # disconnect VPN
    os.system("windscribe disconnect")
    print("sorry, some error has occurred..!!")


Below is the complete code based on the above approach:

Python3




# import required modules
import os
from time import sleep
import random
 
# list of VPN server codes
codeList = ["TR", "US-C", "US", "US-W", "CA", "CA-W",
            "FR", "DE", "NL", "NO", "RO", "CH", "GB", "HK"]
 
try:
 
    # connect to VPN
    os.system("windscribe connect")
    while True:
 
        # assigning a random VPN server code
        choiceCode = random.choice(codeList)
 
        # changing IP after a particular time period
        sleep(random.randrange(120, 300))
 
        # connecting to a different VPN server
        print("!!! Changing the IP Address........")
        os.system("windscribe connect " + choiceCode)
 
except:
 
    # disconnect VPN
    os.system("windscribe disconnect")
    print("sorry, some error has occurred..!!")


Output:

The process to execute the Automated VPN Locator Program in python:

Step 1: Log in to the Windscribe with which you have setup the VPN over the system with the command given below.

windscribe login

Step 2: Execute the file you have created in the above steps with the command below.

python3 gfg.py

NOTE: This will change your system’s IP address randomly. Press Ctrl+c to close the VPN service.

Click here to view a small video for a better understanding of the setup and execution of the VPN Automation program.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads