Shell Script for "Fastest Server" feature

I have used Linux for a while, but I still haven’t yet delved into Bash. How would I write a shell script that would essentially do the “fastest server” feature manually?

It would need to do the following:

  • Ping the list of VyprVPN servers
  • Output results with server’'s names, not IP addresses
  • Display fastest servers on top, slowest on bottom

Update: See MattN’s response for a better implementation than mine

This is what I can type in from the command line and it’s served me well so far (using Linux Mint):

 sudo fping -c 5 -ef myserver.txt

where the file “myserver.txt” is the list of VyprVPN servers with the ones I usually use uncommented and the ones I don’t commented:

# Format:
#   Server name
#   Server IP address (commented = skip; uncommented = check)

# Los Angeles, CA 
us1.vpn.goldenfrog.com

# San Francisco, CA
# us7.vpn.goldenfrog.com

# etc.....

@RunningWolf We would not be able to assist you with writing a script to accomplish this, but It looks like you’re on to something there for sure. We are planning on adding more features for Linux in the future.

If you do find a working solution for Fastest Server via script, feel free to share it here for other users!

Nick
Golden Frog Support

Thanks for the response Nick!

I hope other Linux users might be able to help me improve this, but I also wanted to post it so other users could use it.

Maybe it’s near Christmas, I feel like to throw out some codes :smile: T&Cs: Code quality not guaranteed.

Assuming you have vyprvpn command line client (http://www.goldenfrog.com/community/beta/vyprvpn-linux-cli) and python installed.

Note: the following script does not require sudo or root

  1. Login using vyprvpn command line client first. If you haven’t done so.

    vyprvpn login

  2. Create a python file (e.g. nearest_server.py) and copy and paste following codes.

    import subprocess as sub
    import re
    import Queue
    import threading

    vyprvpn_bin = ‘/usr/bin/vyprvpn’
    ping_count = 4

    dns_re = r’[\w\d]+.vpn.goldenfrog.com’
    ping_re = r’min/avg/max/mdev = ([\d.]+)/([\d.]+)/([\d.]+)/([\d.]+) ms’

    def getPingMsAvg(results, dns):
    p = sub.Popen([‘ping’, dns, ‘-c’, str(ping_count)], stdout=sub.PIPE, stderr=sub.PIPE)
    output, errors = p.communicate()
    m = re.search(ping_re, output, re.M)
    avg_ms = 9999.9
    if m:
    avg_ms = float(m.group(2))
    results.append({ ‘dns’ : dns, ‘ping_avg_ms’ : avg_ms})

    p = sub.Popen([vyprvpn_bin, ‘server’, ‘list’], stdout=sub.PIPE, stderr=sub.PIPE)
    output, errors = p.communicate()

    threads = []
    results = []
    for dns in re.findall(dns_re, output, re.M):
    t = threading.Thread(target=getPingMsAvg, args = (results, dns))
    t.start()
    threads.append(t)

    for t in threads:
    t.join()

    results.sort(cmp=lambda x,y: cmp(x[‘ping_avg_ms’], y[‘ping_avg_ms’]))
    for result in results:
    print “%s: %0.3f ms” % (result[‘dns’], result[‘ping_avg_ms’])

    The following codes set nearest server for you

    nearest_dns = results[0][‘dns’]
    print “Setting nearest server %s as default server” % nearest_dns
    p = sub.Popen([vyprvpn_bin, ‘server’, ‘set’, nearest_dns], stdout=sub.PIPE, stderr=sub.PIPE)
    output, errors = p.communicate()

  3. Run it

    python nearest_server.py

  4. Restart VPN connection

    vyprvpn disconnect
    vyprvpn connect

You should get something like this:

au2.vpn.goldenfrog.com: 17.193 ms
au1.vpn.goldenfrog.com: 17.524 ms
nz1.vpn.goldenfrog.com: 17.662 ms
hk1.vpn.goldenfrog.com: 130.556 ms
sg1.vpn.goldenfrog.com: 131.514 ms
kr1.vpn.goldenfrog.com: 200.426 ms
us7.vpn.goldenfrog.com: 202.375 ms
jp1.vpn.goldenfrog.com: 205.899 ms
us1.vpn.goldenfrog.com: 207.360 ms
us6.vpn.goldenfrog.com: 235.788 ms
mx1.vpn.goldenfrog.com: 242.700 ms
us5.vpn.goldenfrog.com: 247.518 ms
us3.vpn.goldenfrog.com: 247.958 ms
br1.vpn.goldenfrog.com: 255.425 ms
ca1.vpn.goldenfrog.com: 256.114 ms
us4.vpn.goldenfrog.com: 258.022 ms
vn1.vpn.goldenfrog.com: 262.761 ms
th1.vpn.goldenfrog.com: 263.058 ms
au3.vpn.goldenfrog.com: 263.187 ms
id1.vpn.goldenfrog.com: 263.555 ms
us2.vpn.goldenfrog.com: 264.717 ms
my1.vpn.goldenfrog.com: 264.723 ms
ph1.vpn.goldenfrog.com: 267.139 ms
is1.vpn.goldenfrog.com: 327.027 ms
fr1.vpn.goldenfrog.com: 333.634 ms
at1.vpn.goldenfrog.com: 334.134 ms
se1.vpn.goldenfrog.com: 340.674 ms
ie1.vpn.goldenfrog.com: 342.939 ms
uk1.vpn.goldenfrog.com: 348.427 ms
es1.vpn.goldenfrog.com: 348.987 ms
lt1.vpn.goldenfrog.com: 349.590 ms
be1.vpn.goldenfrog.com: 350.341 ms
ch1.vpn.goldenfrog.com: 353.713 ms
lu1.vpn.goldenfrog.com: 355.510 ms
it1.vpn.goldenfrog.com: 356.644 ms
eu1.vpn.goldenfrog.com: 357.661 ms
fi1.vpn.goldenfrog.com: 357.883 ms
cz1.vpn.goldenfrog.com: 357.946 ms
dk1.vpn.goldenfrog.com: 358.482 ms
ro1.vpn.goldenfrog.com: 359.084 ms
tr1.vpn.goldenfrog.com: 359.249 ms
pt1.vpn.goldenfrog.com: 359.441 ms
pl1.vpn.goldenfrog.com: 360.423 ms
ru1.vpn.goldenfrog.com: 360.669 ms
de1.vpn.goldenfrog.com: 360.685 ms
no1.vpn.goldenfrog.com: 364.664 ms
Setting nearest server au2.vpn.goldenfrog.com as default server

Hope this will help you. HoHoHo

1 Like

Hello Matt,

Thank you for the share! We will be happy to check this out, and I’m certain our Linux users looking for fastest server functionality will be excited as well. Happy holidays to you!

Regards,
Keegan
Golden Frog Support Supervisor

Wonderful!

Worked fine for me.

Merry Xmas!

Same idea using fping and bash:

#!/bin/bash

1. make sure you have fping installed

sudo apt-get install fping

2. make sure you are already logged in

vyprvpn login

vyprvpn disconnect
vyprvpn refresh

TOP5=$(vyprvpn server list | awk ‘{print $1}’ | grep “.vpn.goldenfrog.com” | fping -e -c 5 -q 2>&1 | awk -F’[ /]’ ‘{print $16" "$1}’ | sort -n | head -5)
echo “Top 5 Servers”
echo “$TOP5”

vyprvpn server set $(echo $TOP5 | head -1 | awk ‘{print $2}’)
vyprvpn connect

Hello @spraguep,

We appreciate you sharing this, as well as our other Linux users I am sure. We will check this out ourselves, as we continue to look for ways to improve our VyprVPN CLI.

Regards,
Samuel
Golden Frog Support

I know this is an old thread but I’ve found this googling. Scrpit came from here. Works wonderful.

[quote] fast-server.sh
#!/bin/bash

add ip / hostname separated by white space

HOSTS=“us1.vpn.goldenfrog.com us2.vpn.goldenfrog.com us3.vpn.goldenfrog.com us4.vpn.goldenfrog.com us5.vpn.goldenfrog.com us6.vpn.goldenfrog.com us7.vpn.goldenfrog.com

Smallest=“200000”
Server=""

for myHost in $HOSTS
do
Current1=ping -c 4 -q -i .2 $myHost | grep avg | awk -F'/' '{print $5}'
Current=${Current1/.*/}

#echo $myHost $Current
if [ “$Current” -lt “$Smallest” ]
then
Server=$myHost
Smallest=$Current
fi
done
echo “Smallest = $Smallest @ $Server”
[/quote]

I just tried this nearest_server.py and after following instructions I received the following:
$ vyprvpn o
Logout result: Logout completed
$ vyprvpn l
Username:
Password:
Login result: Authorized
$ python nearest_server.py
Traceback (most recent call last):
File “nearest_server.py”, line 39, in
nearest_dns = results[0][‘dns’]
IndexError: list index out of range
$ vyprvpn c
Success! VyprVPN Connected

So it succeeded, but oddly changed the protocol from Chameleon, which had been my default, to OpenVPN256. The server remained unchanged but the one that was default was almost always the quickest anyway.

I don’t know python. Any ideas?

Hey @sturbs!

I tested Matt’s script just now and experienced different results. It looks like you received some error output, which I wasn’t able to replicate. I suspect it may not have actually worked. Here’s what I did:

  1. Create a new file for the script: touch fastest_server.py
  2. Edit the file: nano fastest_server.py
  3. Paste the contents of Matt’s script into the file and saved it. (Spacing counts, so needs to be pasted properly)
  4. Logged in: vyprvpn login
  5. Set my protocol to Chameleon: vyprvpn protocol set (4 for Chameleon)
  6. Ran the script: python fastest_server.py
  7. It selected mx1, then I connected: vyprvpn c
  8. Checked protocol setting again and verified it was still set to Chameleon: vyprvpn protocol show

Example output:

nickb@ubuntu:~/Desktop$ vyprvpn protocol set
VyprVPN Protocols
1: pptp
2: openvpn160
3: openvpn256
4: chameleon
Please enter the number of your selection:
4
Success: chameleon set as new default
nickb@ubuntu:~/Desktop$ python fastest_server.py

***LIST OF SERVERS AND PING TIMES HERE, REMOVED FOR BREVITY***

Setting nearest server mx1.vpn.goldenfrog.com as default server
nickb@ubuntu:~/Desktop$ vyprvpn c
Success! VyprVPN Connected
nickb@ubuntu:~/Desktop$ vyprvpn protocol show
chameleon

Just wanted to share my experience. Perhaps try remaking the file and running the script again. Let me know if you have success. FYI - I am a Python noob myself, but in looking at the script, I am not seeing anything that sets the preferred protocol.

Nick
Golden Frog Support

For users not so used to linux
Useful bash script for fast connecting if you are always going to want to connect to same server.
Have hashed out the password parts as bad practice to keep passwords in plain text.

Customise as you please i know I’m in the UK so that will most of the time be the sever I want to connect to.
Save the following to a .sh file can be any name you like.

#!/bin/bash
email=''
#password=''
server='uk1.vpn.goldenfrog.com'
protocol='openvpn256'
vyprvpn l $email #$password
vyprvpn server set $server
vyprvpn protocol set $protocol
vyprvpn connect

Once you create a file need to set it to executable
chmod +x FILENAME.sh

run it with the following
sh FILENAME.sh

Have added protocol as a variable in hope they add more

I know its nothing fancy but hopefully will save someone time who wants something quick and simple

Thanks for taking the time to post this script here! I’ll bet other users will find this useful.