-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv-cli.py
84 lines (72 loc) · 2.63 KB
/
ipv-cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from zipfile import ZipFile
import zipfile
import wget
import os
import subprocess
import pycountry
# define variables
path = '/path/ipvanish/'
url = 'https://www.ipvanish.com/software/configs/configs.zip'
filename = path + '/' + os.path.basename(url)
best_ping = 99999
# get user's choice
def get_choice():
print("1 - delete old configs, and download + unzip new config\n2 - check best server to desired country\n3 - exit")
choice = int(input("Enter number:\n"))
return (choice)
# get country from user input
def get_country():
print("Please enter the name of the country you would like to connect to")
country = str(input("Country: "))
return(country)
# convert user selection into 2 letter country notion
def get_country_code(country):
mapping = {country.name: country.alpha_2 for country in pycountry.countries}
code = mapping.get(country)
if type(code) is str:
return(code)
else:
return("Country not found!")
# deletes old *.zip & *.ovpn files, downloads new file and unzips it
def delete_and_renew_config():
for config_file in os.listdir(path):
if config_file.endswith(".ovpn") or config_file.endswith(".crt") or config_file == "configs.zip":
os.remove(path + config_file)
wget.download(url, '/path/ipvanish/configs.zip')
with zipfile.ZipFile(filename, 'r') as zip_ref:
zip_ref.extractall(path)
# returns array [host, avg ping to host]
def get_host_and_ping(conf_file):
with open(path + conf_file) as file:
for i, line in enumerate(file):
if i == 3:
host = line.split()[1]
ping = os.system("ping -c 1 " + host)
return [host, ping]
# returns dictionary with the form: {"filename.ovpn" : [ "host", avgping]}
def get_servers(code):
srvs = {}
for config_file in os.listdir(path):
if config_file.startswith("ipvanish-" + code):
# first 0 is placeholder for HOSTNAME, second is for AVG PING
srvs[config_file] = get_host_and_ping(config_file)
return(srvs)
# returns the config file name of the best server
def return_best_server(servers):
for index, (server, host) in enumerate(servers.items()):
if float(host[1]) < float(best_ping):
best_srv = server
return(best_srv)
# main while loop
while 1:
choice = get_choice()
if choice == 1:
delete_and_renew_config()
elif choice == 2:
country = get_country()
code = get_country_code(country)
servers = get_servers(code)
best_srv = return_best_server(servers)
print("The best server is: " + best_srv)
elif choice == 3:
break