None of us will every accomplish anything excellent or commanding except when he listens to this whisper which is heard by him alone.”
~Ralph Waldo Emerson

Google+ Pinterest LinkedIn Tumblr +

This one I’m pretty proud of and use quite often.  NordVPN is a strong VPN provider that has thousands of servers all around the world that are OpenVPN compatible.  Windows. Mac and smartphone users have it easy as they have built-in apps to find the nearest endpoint, but Linux users are on their own.

This multithreaded program will automatically download the latest list of endpoints and find the one nearest you.

import os
import shutil
import re
import subprocess
import datetime
import apt
import collections

FNULL = open(os.devnull, 'w')
nordcurrentzip = ''
curdate = ("%m%d%y.%M%S"))
homedir = '/tmp/nordcheck-' + curdate
configzip = homedir + '/'

norddict = {
    'filename' : ['','10.000']

windict = {
    '1st' : ['name', '10.000'],
    '2nd' : ['name', '11.000'],
    '3rd' : ['name', '12.000']

def cleanup():
    global homedir

def errout (problem):
    print (problem)

def makehome ():
    global homedir
        errout("unable to make dir")

def getnordzip():
    global nordcurrentzip
    cache = apt.Cache()
    if not cache["wget"].is_installed:
        errout("wget not installed, need to 'apt-get install wget' - exiting")

    print ("Downloading current .ovpn archive..")
    wgetresult = (['wget', nordcurrentzip, '-O', configzip], stdout=FNULL, stderr=subprocess.STDOUT)
    if wgetresult != 0:
        errout("Unable to download " + configzip + ", exiting")
        print ("Decompressing " + configzip + "...")
        unzipresult = (['unzip', configzip, '-d', homedir], stdout=FNULL, stderr=subprocess.STDOUT)
        if unzipresult != 0 and unzipresult != 2:
            errout("Can't unzip " + configzip + ", exiting")

def loaddict():
    global norddict, homedir

    for parsefile in os.listdir(homedir):
        if parsefile.endswith("tcp443.ovpn"):
            with open(homedir + "/" + parsefile) as ovpnfile:
                for line in ovpnfile:
                    if "remote " in line:
                        line = line.split()
                        ipaddr = line[1]
                        norddict[parsefile]= [ipaddr,'']

def getlatency():
    global norddict
    global windict
    bestscore = 1000.000

    print ("Testing: ")
    for fname, attrib in norddict.items():
        print (fname + " (" + attrib[0] + "): ", end='')
        response = subprocess.Popen (['ping', '-nq', '-c 2', '-W 1', attrib[0]], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, error = response.communicate()
        if "100% packet loss," in str(out):
            norddict[fname]= [attrib[0],int(1000.000)]
            print ("1000.00")
            output = str(out).split('/')
            norddict[fname]= [attrib[0],output[4]]
            print (output[4])

            if float(output[4]) < bestscore:
                bestscore = float(output[4])
                windict['3rd'] = windict['2nd']
                windict['2nd'] = windict['1st']
                windict['1st'] = [[fname],output[4]]

def showresults():
    global norddict
    global windict

    ordereddict = collections.OrderedDict(sorted(windict.items()))
    print ("")
    for fname, attrib in ordereddict.items():
        print (str(attrib[0]) + ": " + str(attrib[1]))


print ('original archive located in ' + str(homedir))

Leave A Reply

Secured By miniOrange