2

I was wondering how to use NordVPN servers to change IP address during my web automation with Python. Let's say to use it with mechanize this way:

br.open("url", proxies="nordvpn proxy")

Thanks in advance.

JawadMans
  • 63
  • 1
  • 7
  • This post can help you with that: https://stackoverflow.com/questions/3168171/how-can-i-open-a-website-with-urllib-via-proxy-in-python – Enrique Tejeda Aug 08 '21 at 18:17

2 Answers2

4

I know this might be a bit late, but recently I encountered a challenge similar to your question.

Generally, I am using python to execute Nord's Linux commands.

import subprocess
import time
import subprocess, re, random, datetime

    # Nordvpn shinanigance skip to line 105
def getCountries():
    """
    This function will return a list of the current countries with available servers for your nordvpn account.
    """
    nord_output = subprocess.Popen(["nordvpn", "countries"], stdout=subprocess.PIPE)
    countries = re.split("[\t \n]", nord_output.communicate()[0].decode("utf-8"))
    while "" in countries:
        countries.remove("")
    return countries

def chooseRandom(country_list):
    """
    This function will randomly choose a country out of the available countries list.
    """
    return country_list[random.randrange(0, len(country_list))]

def logIn(random_country):
    """
    This function will take the randomly chosen country and attempt to log in to NordVPN using that country.
    """
    print("{} has been selected as the random country.".format(random_country))
    # subprocess.call(["nordvpn", "c", random_country])
    cmd = ["nordvpn", "c", random_country]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    o, e = proc.communicate()
    if "Whoops! We couldn't connect you" in o.decode('ascii'):
        print("not connected. Retrying...")
        logIn(chooseRandom(getCountries()))
    else:
        print("Connected to {}".format(random_country))

def checkConnection():
    print("checkConnection connection!")
    cmd = ['nordvpn', 'c']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    o, e = proc.communicate()
    if "Connected" in o.decode('ascii'):
        return True
    else:
        return False

def tryConnection():
    print("Trying connection!")
    cmd = ['nordvpn', 'c']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    time.sleep(2)
    if checkConnection() == True:
        print("Sucessfull Connection with:\n" + email +" " +password)
    else:
        print("Failed")
        cmd = ['nordvpn', 'logout']
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        print("Logged out")

def loginWithPassword(email, password):
    cmd = ['nordvpn', 'login', '--username', email, '--password', password]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # print(proc)
    o, e = proc.communicate()
    stdout = proc.communicate()[0]
    # print (format(stdout))
    if "is not correct" in o.decode('ascii'):
        print("Wrong password")

    if "already logged in" in o.decode('ascii'):
        print("Already logged in")
        nord_output = subprocess.Popen(["nordvpn", "status"], stdout=subprocess.PIPE)
        status = re.split("[\r \n :]", nord_output.communicate()[0].decode("utf-8"))[-2]
        if status == "Disconnected":
            print("Disconnected from nord!")
            logIn(chooseRandom(getCountries()))
        else:
            print("Connected...")
            print("Calling diconnecting to randomize...")
            subprocess.call(["nordvpn", "disconnect"])
            logIn(chooseRandom(getCountries()))

So, you just have to call

loginWithPassword("email", "password")

Massive thanks to this guy whom I borrowed a few things from

NordVPN_Randomizer

  • i got this error: `FileNotFoundError: [Errno 2] No such file or directory: 'nordvpn'` any idea? – yts61 Nov 28 '21 at 22:43
  • 2
    You have to install nordvpn first. Follow this link on how to install nordvpn https://support.nordvpn.com/Connectivity/Linux/1325531132/Installing-and-using-NordVPN-on-Debian-Ubuntu-Raspberry-Pi-Elementary-OS-and-Linux-Mint.htm – Sikini Joseph Dec 13 '21 at 19:59
  • is there a way to retrieve an up-to-date list of nordvpn servers directly via an API call or a http url? – user1050755 Aug 25 '23 at 10:40
0

Here is a simple way to do it using Python plus Command Line (if you are using Windows):

import os

current = os.getcwd() #get working directory
os.chdir("C:\\Program Files\\NordVPN") #cd into nord directory
os.system("nordvpn -c -g 'United States'") #change ip
os.chdir(current) #cd back into working directory
Ned Hulton
  • 477
  • 3
  • 12
  • 27