Skip to main content

Automatically reconnect/log in on headless linux server

PreReqs:

  1. Create a login token for your NordVPN account

  2. While connected to NordVPN on your server, run the ip link show command in your console and determine the name of the interface used by NordVPN when it is active. If using NordLynx, the interface is likely to be named nordlynx. Be sure to note down the name as you'll need it for the script

Using the script:

#!/bin/bash

date=$(date)
#Log path example: /home/username/nord_reconnection.txt
log_path=PATH_WHERE_TO_LOG_MESSAGES
nord_token=YOUR_NORDVPN_LOGIN_TOKEN
# Uncomment webhook if you want to use a notification system
#webhook="YOUR WEBHOOK IN QUOTATION MARKS"
# While nordvpn is connected, run ip link show to determine the name of the interface used by nordvpn (for example nordlynx)
link_name=nordlynx

count=$( ip link show | grep $link_name | wc -l )
if [ $count -eq 0 ]; then
        echo "-----------------------------------------------" >> $log_path
        echo "Checking if logged in" >> $log_path
        if echo `nordvpn connect` | grep -q "You are not logged in."; then
                echo "NordVPN logging in: $date" >> $log_path
                echo `nordvpn login --token $nord_token`
                echo "Attempting to connect again: $date" >> $log_path
                if echo `nordvpn connect` | grep -q "You are not logged in."; then
                        echo "Login was unsuccessful" >> $log_path
			if [[ -v webhook ]]; then
                        	echo `curl -X POST $webhook`
			fi
                else
                        echo "Login and connection successful" >> $log_path
                fi
        else
                echo "Successfully connected" >> $log_path
        fi
fi
  1. Edit the log_path variable with the path of where you want outputs from the script to go

  2. Place your login token in the nord_token variable

  3. Optional: Uncomment the webhook variable and place your webhook inside of the quotation marks

  4. If needed, replace nordlynx in the link_name variable with the interface name you acquired in the prereq steps.

  5. Save the script to your server and schedule it as a cron job