Skip to content

Instantly share code, notes, and snippets.

@Lazza
Last active September 20, 2025 00:34
Show Gist options
  • Save Lazza/bbc15561b65c16db8ca8 to your computer and use it in GitHub Desktop.
Save Lazza/bbc15561b65c16db8ca8 to your computer and use it in GitHub Desktop.

Revisions

  1. Lazza revised this gist Nov 26, 2022. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,15 @@
    # This script is NOT MAINTAINED

    This snippet of code was posted in 2014 and slightly revised in 2016 and 2017. It was more of a quick'n'dirty script than a polished tool. It is made **only for Linux** and in Python 2, which has since become outdated.

    I currently do not use it, and I suggest you avoid it as well. **Please do not expect support for using this script.**

    🔥 If you need an alternative, [@glaucocustodio](https://github.com/glaucocustodio) has kindly suggested [EasyVPN](https://github.com/rodrigogs/easyvpn) in [this comment](https://gist.github.com/Lazza/bbc15561b65c16db8ca8?permalink_comment_id=4381847#gistcomment-4381847).

    The rest of the README is left for historical purposed.

    ----

    # vpngate.py

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.
  2. Lazza revised this gist Jun 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion vpngate.py
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@
    supported = [s for s in desired if len(s[-1]) > 0]
    print str(len(supported)) + ' of these servers support OpenVPN'
    # We pick the best servers by score
    winner = sorted(supported, key=lambda s: s[2], reverse=True)[0]
    winner = sorted(supported, key=lambda s: float(s[2].replace(',','.')), reverse=True)[0]

    print "\n== Best server =="
    pairs = zip(labels, winner)[:-1]
  3. Lazza revised this gist May 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion vpngate.py
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@

    f = open(path, 'w')
    f.write(base64.b64decode(winner[-1]))
    f.write('\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf')
    f.write('\nscript-security 2\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf')
    f.close()

    x = subprocess.Popen(['sudo', 'openvpn', '--config', path])
  4. Lazza revised this gist May 17, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,7 @@

    f = open(path, 'w')
    f.write(base64.b64decode(winner[-1]))
    f.write('\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf')
    f.close()

    x = subprocess.Popen(['sudo', 'openvpn', '--config', path])
  5. Lazza revised this gist Aug 31, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@
    i = 5 # long name for country
    else:
    print 'Country is too short!'
    exit(1)

    try:
    vpn_data = requests.get('http://www.vpngate.net/api/iphone/').text.replace('\r','')
  6. Lazza revised this gist Aug 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Description
    # vpngate.py

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.

  7. Lazza revised this gist Aug 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ Moreover, the script allows to input countries with **any case** (`Italy`, `ital

    Here is a short Youtube video showcasing an example usage:

    [![YT video](http://img.youtube.com/vi/3OFwxkxN_HI/0.jpg)](http://youtu.be/3OFwxkxN_HI)
    [![YT video](http://i.imgur.com/WxbOiOT.png)](http://youtu.be/3OFwxkxN_HI)

    # Requirements

  8. Lazza revised this gist Aug 30, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,8 @@


    if len(sys.argv) != 2:
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    country = sys.argv[1]

    if len(country) == 2:
    @@ -38,7 +38,7 @@
    found = len(desired)
    print 'Found ' + str(found) + ' servers for country ' + country
    if found == 0:
    exit(1)
    exit(1)

    supported = [s for s in desired if len(s[-1]) > 0]
    print str(len(supported)) + ' of these servers support OpenVPN'
    @@ -71,6 +71,6 @@
    x.kill()
    except:
    pass
    while x.poll() != 0:
    time.sleep(1)
    print '\nVPN terminated'
    while x.poll() != 0:
    time.sleep(1)
    print '\nVPN terminated'
  9. Lazza created this gist Aug 30, 2014.
    32 changes: 32 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # Description

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.

    After this step, OpenVPN is launched with the proper configuration. The VPN can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    # Usage

    Run the script by providing the desired output country:

    vpngate.py US

    Both country codes and country names are supported, as listed on the VPNGate website, e.g.:

    vpngate.py "United Kingdom"

    Moreover, the script allows to input countries with **any case** (`Italy`, `italy`, `ItALy` all work) and with **partial names**:
    - `Korea` will work for `Korea Republic Of`
    - `Russia` will work for `Russian Federation`
    - ... and so on

    # Demo

    Here is a short Youtube video showcasing an example usage:

    [![YT video](http://img.youtube.com/vi/3OFwxkxN_HI/0.jpg)](http://youtu.be/3OFwxkxN_HI)

    # Requirements

    OpenVPN needs to be installed.

    The script should run on any Linux distribution with the Python [Requests](python-requests.org) module installed. The user running the script must be able to run `sudo` commands in order to start `openvpn`.
    76 changes: 76 additions & 0 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/usr/bin/env python

    """Pick server and start connection with VPNGate (http://www.vpngate.net/en/)"""

    import requests, os, sys, tempfile, subprocess, base64, time

    __author__ = "Andrea Lazzarotto"
    __copyright__ = "Copyright 2014+, Andrea Lazzarotto"
    __license__ = "GPLv3"
    __version__ = "1.0"
    __maintainer__ = "Andrea Lazzarotto"
    __email__ = "[email protected]"


    if len(sys.argv) != 2:
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    country = sys.argv[1]

    if len(country) == 2:
    i = 6 # short name for country
    elif len(country) > 2:
    i = 5 # long name for country
    else:
    print 'Country is too short!'

    try:
    vpn_data = requests.get('http://www.vpngate.net/api/iphone/').text.replace('\r','')
    servers = [line.split(',') for line in vpn_data.split('\n')]
    labels = servers[1]
    labels[0] = labels[0][1:]
    servers = [s for s in servers[2:] if len(s) > 1]
    except:
    print 'Cannot get VPN servers data'
    exit(1)

    desired = [s for s in servers if country.lower() in s[i].lower()]
    found = len(desired)
    print 'Found ' + str(found) + ' servers for country ' + country
    if found == 0:
    exit(1)

    supported = [s for s in desired if len(s[-1]) > 0]
    print str(len(supported)) + ' of these servers support OpenVPN'
    # We pick the best servers by score
    winner = sorted(supported, key=lambda s: s[2], reverse=True)[0]

    print "\n== Best server =="
    pairs = zip(labels, winner)[:-1]
    for (l, d) in pairs[:4]:
    print l + ': ' + d

    print pairs[4][0] + ': ' + str(float(pairs[4][1]) / 10**6) + ' MBps'
    print "Country: " + pairs[5][1]

    print "\nLaunching VPN..."
    _, path = tempfile.mkstemp()

    f = open(path, 'w')
    f.write(base64.b64decode(winner[-1]))
    f.close()

    x = subprocess.Popen(['sudo', 'openvpn', '--config', path])

    try:
    while True:
    time.sleep(600)
    # termination with Ctrl+C
    except:
    try:
    x.kill()
    except:
    pass
    while x.poll() != 0:
    time.sleep(1)
    print '\nVPN terminated'