check status connected nordvpn bash script

34 Views Asked by At

I am trying to check if my computer is connected with VPN (NORDVPN)

My script in bash is the following:

STATUS = "$(nordvpn status | head -n 1)"
printf "STATUS %s\n" "$STATUS"

if [[ $STATUS == "Status: Disconnected" ]]
then
        echo "Disconnected from VPN..."
        nordvpn connect Salt_Lake_City
fi

I get the following output:

STATUS: command not found
STATUS 

Thanks in advance

2

There are 2 best solutions below

0
Gilles Quénot On BEST ANSWER

What I would do:

status="$(nordvpn status | head -n 1)"
printf "STATUS %s\n" "$status"

if [[ $status == "Status: Disconnected" ]]
then
    echo "Disconnected from VPN..."
    nordvpn connect Salt_Lake_City
fi

or

status="$(nordvpn status)"
printf "STATUS %s\n" "$status"

if [[ $status == "Status: Disconnected"* ]]
then
    echo "Disconnected from VPN..."
    nordvpn connect Salt_Lake_City
fi
1
grafeno30 On

At the end, I have done some simply changes.

I have used the keyword SET, I have used backticks.....

set status = `nordvpn status | head -n 1`
printf "STATUS %s" "$status"

if [[ $status == "Status: Disconnected" ]]
then
        echo "Disconnected from VPN..."
        nordvpn connect Salt_Lake_City
else
        echo "Connected..."
fi

**Thanks to everybody **