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 os
from time import sleep
import random
Step 3: Create a list of Free VPN server codes provided by Windscribe (VPN).
Python3
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:
os.system("windscribe connect")
while True:
choiceCode = random.choice(codeList)
sleep(random.randrange(120, 300))
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:
os.system("windscribe disconnect")
print("sorry, some error has occurred..!!")
Below is the complete code based on the above approach:
Python3
import os
from time import sleep
import random
codeList = ["TR", "US-C", "US", "US-W", "CA", "CA-W",
"FR", "DE", "NL", "NO", "RO", "CH", "GB", "HK"]
try:
os.system("windscribe connect")
while True:
choiceCode = random.choice(codeList)
sleep(random.randrange(120, 300))
print("!!! Changing the IP Address........")
os.system("windscribe connect " + choiceCode)
except:
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.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
04 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...