How to Automate VPN to change IP location on Ubuntu using Python?
Last Updated :
04 Jul, 2021
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.
Similar Reads
Automate linkedin connections using Python Automating LinkedIn connections using Python involves creating a script that navigates LinkedIn, finds users based on specific criteria (e.g., job title, company, or location), and sends personalized connection requests. In this article, we will walk you through the process, using Selenium for web a
5 min read
Telnet Automation / Scripting Using Python Telnet is the short form of Teletype network, which is a client/server application that works based on the telnet protocol. Telnet service is associated with the well-known port number - 23. As Python supports socket programming, we can implement telnet services as well. In this article, we will lea
5 min read
Build a GUI Application to ping the host using Python Prerequisite: Python GUI â Tkinter In this article, we are going to see how to ping the host with a URL or IP using the python ping module in Python. This module provides a simple way to ping in python. And It checks the host is available or not and measures how long the response takes. âBeforeâ sta
2 min read
Create a GUI to find the IP for Domain names using Python Prerequisite: Python GUI â tkinterIn this article, we are going to see how to find IP from Domain Names and bind it with GUI Application using Python. We will use iplookup module for looking up IP from Domain Names. It is a small module which accepts a single domain as a string, or multiple domains
2 min read
How to Execute Shell Commands in a Remote Machine using Python - Paramiko Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement for SSL to make a secure connection between two devices. It also supports the SFTP client and server model. Authenticating SSH connection To authenticate an SSH connection,
4 min read
View Computer's Important Network Information Using Python While working in a network, we do some troubleshooting with the network or Internet problem. This time we need to check your own system network connection information. We can find the network connection in the Control Panel in Windows. The best way to find this information we can use ipconfig comman
1 min read