Skip to content

Instantly share code, notes, and snippets.

@aivanise
Created July 5, 2021 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aivanise/58226db6491f3339cbfa645a9dc310c0 to your computer and use it in GitHub Desktop.
Save aivanise/58226db6491f3339cbfa645a9dc310c0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# nordvpn control script for debian/ubuntu
# extra packages needed: fzf jq
# run without paramaters - present a picker with countries list, them pick a random server from that country
# vpn country - pick a random server from specified country
# vpn stop/start/status - run systemctl stop/start/status
# vpn config - see configured country
# put your nordvpn user/pass in /etc/openvpn/client/nordvpn
# https://nordvpn.com/servers/tools/
# https://sleeplessbeastie.eu/2019/02/18/how-to-use-public-nordvpn-api/
case "$1" in
start|stop|status) systemctl $1 openvpn-client@nordvpn ;;
config) fgrep '#config' /etc/openvpn/client/nordvpn.conf ;;
*)
country=$1
[[ -z "$country" ]] && country=$(curl --silent https://api.nordvpn.com/server | jq --raw-output '[.[].country] | sort | unique | .[]' | fzf)
server=$(curl --silent "https://api.nordvpn.com/v1/servers?limit=16384" | jq --raw-output '.[] | select(.locations[].country.name == "'$country'") | .hostname' | shuf -n 1)
[[ -z "$server" ]] && echo "No server found in $country" && exit 1
systemctl stop openvpn-client@nordvpn
curl -m 5 -sS -o /etc/openvpn/client/nordvpn.conf https://downloads.nordcdn.com/configs/files/ovpn_udp/servers/$server.udp.ovpn
echo '#config '$country $server >> /etc/openvpn/client/nordvpn.conf
echo auth-user-pass nordvpn >> /etc/openvpn/client/nordvpn.conf
# route back cloudflare
for net in $(curl -Ss -m 5 https://www.cloudflare.com/ips-v4); do
echo route $(ipcalc --nocolor --nobinary $net | awk '/Address:/ { print $2 } /Netmask:/ { print $2} ' | xargs echo) net_gateway >> /etc/openvpn/client/nordvpn.conf
done
# route back our own ip, in case you have port forwards from the router
echo route $(curl -sS -m 5 https://api.ipify.org?format=text) 255.255.255.255 net_gateway >> /etc/openvpn/client/nordvpn.conf
systemctl start openvpn-client@nordvpn
sleep 3
systemctl status openvpn-client@nordvpn
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment