summarylogtreecommitdiffstats
path: root/nordvpn
blob: 438b538b4605bc0c2bb541a89f7eb57b9635b162 (plain)
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
#!/usr/bin/sh

get_service() {
    systemctl --type=service | grep openvpn-client@nordvpn | awk '{print $1}'
}

run_ping() {
    remote=$(grep -E '^remote [0-9.]+ [0-9]+$' $1 | cut -d ' ' -f 2)
    test ! -z "$remote" || exit 1
    bin=$(which ping)
    test $? -eq 0 || echo "ping not found, please install iputils"
    $bin -c 4 -A $remote | tail -n 1 | cut -d / -f 5
}

command=$1
shift

case $command in
    list)
        find /etc/openvpn/client/ -type l -name "nordvpn_*${1}*.conf" \
             | xargs -L1 basename \
             | cut -d _ -f 2 \
             | cut -d . -f 1 \
             | sort -g
        ;;
    ping)
        file=/etc/openvpn/client/nordvpn_${1}.conf
        test -f "$file" || exit 1
        run_ping $file
        ;;
    rank)
        tmp=$(mktemp)
        for f in $(find /etc/openvpn/client/ -type l -name "nordvpn_*${1}*.conf")
        do
            echo $(basename $f .conf | cut -d _ -f 2) $(run_ping $f) >> $tmp
        done
        sort -k 2 -n $tmp
        rm -f $tmp
        ;;
    status)
        service=$(get_service)
        test -z "$service" || systemctl status $service
        ;;
    start)
        service=$(get_service)
        test -z "$service" || $0 stop
        systemctl $command openvpn-client@nordvpn_${1}
        ;;
    stop|restart)
        service=$(get_service)
        test -z "$service" || systemctl $command $service
        ;;
    *)
        echo "usage: $(basename $0) command [options]"
        echo
        echo "Available commands:"
        echo "    list [server_name_pattern]"
        echo "        List available servers."
        echo "    ping server_name"
        echo "        Show round trip latency"
        echo "    rank [server_name_pattern]"
        echo "        Ping all servers matching pattern and rank them"
        echo "    status"
        echo "        Show current systemd service status, if any."
        echo "    start|stop|restart server_name"
        echo "        Start, stop or restart systemd service for specified server."
        ;;
esac