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

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.
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 modulesimport osfrom time import sleepimport random
 
 
Step 3: Create a list of Free VPN server codes provided by Windscribe (VPN).
 
Python3
# list of VPN server codescodeList = ["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..!!")
Python3
# import required modulesimport osfrom time import sleepimport random # list of VPN server codescodeList = ["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:
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.
saurabh1990aror
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.