[Solved] IF THEN to check expressvpn status

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

[Solved] IF THEN to check expressvpn status

Post by Sugarcrisp »

I'm new to Linux and Bash and I'm trying to create a script to check my expressvpn status to verify that I'm connected. If not connected, then connect. After searching Google about IF THEN bash scripts, I came up with the following simple script that I belief says if expressvpn is true (do nothing), if false run expressvpn connect smart. When I run it, I get
Line 2: [expressvpn: unary operator expected]
I looked this up, but it doesn't make sense to me. The script continues and if expressvpn is not connected, it runs the
expressvpn connect smart
command as I want it to. Is this a bug, or do I need to correct something in my script?

Code: Select all

#!/bin/sh
if [ expressvpn status ]
then
        :
else
        expressvpn connect smart
fi
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
WharfRat

Re: IF THEN to check expressvpn status

Post by WharfRat »

What do you get in the terminal when you enter expressvpn status then echo $?
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp »

If I run
expressvpn status
it returns
Disconnected
if there is no connection.

It returns
Connect to <location>
if I'm connected.
WharfRat

Re: IF THEN to check expressvpn status

Post by WharfRat »

There's two ways to do this:

Check for a non 0 return status (I'm guessing that it will return 0 if connected)

Code: Select all

#!/bin/sh
expressvpn status>/dev/null 2>&1
if ! [ $? -eq 0 ];then
	expressvpn connect smart
fi
exit $?
or grep the results for Disconnected

Code: Select all

#!/bin/sh
if expressvpn status|grep -q Disconnected ;then 
	expressvpn connect smart
fi
exit $?
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp »

WharfRat...I tried both of the examples that you provided and they didn't return any results. I noticed one error in the data I provided. Instead of showing Disconnected, it shows Not connected. I made that change to your example before I ran it. My script works, I'm just not sure what the error is.
WharfRat

Re: IF THEN to check expressvpn status

Post by WharfRat »

Can you paste back the actual terminal results like this

Code: Select all

[bill@XPS] ~ $ expressvpn status;echo $?
expressvpn: command not found
127
[bill@XPS] ~ $ 

from these
expressvpn status;echo $?

expressvpn status|grep Not
blockhead47
Level 3
Level 3
Posts: 140
Joined: Wed Jun 15, 2016 4:50 pm

Re: IF THEN to check expressvpn status

Post by blockhead47 »

I am an expressvpn user.

For @Sugarcrisp there is a panel app called "VPN Look-Out" if your are using the Cinnamon desktop.
It does what you are trying to do and much more.

NOTE: The "expressvpn status" command always returns zero ( 0 ) for an exit code.

Here is the output of "disconnected" status:

Code: Select all

$ expressvpn disconnect ; echo $?
Disconnecting...
Disconnected.
0
The output of a "Connected" state:

Code: Select all

$ expressvpn status ; echo $?
Connected to USA - Seattle

   - If your VPN connection unexpectedly drops, internet traffic will be blocked to protect your privacy.
   - To disable Network Lock, disconnect ExpressVPN then type 'expressvpn preferences set network_lock off'.
0

Code: Select all

$ expressvpn --version
expressvpn version 1.4.1 (ec238e1)
Last edited by blockhead47 on Thu Apr 05, 2018 11:13 am, edited 1 time in total.
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp »

expressvpn status;echo $?

connected

Code: Select all

brett@brett-K501UX:~/my-scripts$ expressvpn status;echo $?
Connected to Hong Kong - 4

   - If your VPN connection unexpectedly drops, internet traffic will be blocked to protect your privacy.
   - To disable Network Lock, disconnect ExpressVPN then type 'expressvpn preferences set network_lock off'.
not connected

Code: Select all

brett@brett-K501UX:~/my-scripts$ expressvpn status;echo $?
Not connected
0
expressvpn status|grep Not

connected

Code: Select all

brett@brett-K501UX:~/my-scripts$ expressvpn status|grep Not
brett@brett-K501UX:~/my-scripts$ 
not connected

Code: Select all

brett@brett-K501UX:~/my-scripts$ expressvpn status|grep Not
Not connected
WharfRat

Re: IF THEN to check expressvpn status

Post by WharfRat »

OK then the grep for Not should work.

Use this script, disconnect and paste back the terminal results

Code: Select all

#!/bin/sh
set -x
if expressvpn status|grep -q Not ;then 
	expressvpn connect smart
fi
exit $?
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

Re:IF THEN to check expressvpn status

Post by Sugarcrisp »

Hey WharfRat...That worked without any errors. Thanks. Any idea why I was receiving the error even though it seemed to be working?
Last edited by Sugarcrisp on Thu Apr 05, 2018 11:44 am, edited 1 time in total.
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp »

Hey Blockhead47...Thanks for the post. I will check this out as it sounds good.
WharfRat

Re: Re:IF THEN to check expressvpn status

Post by WharfRat »

Sugarcrisp wrote: Thu Apr 05, 2018 11:41 am Hey WharfRat...That worked without any errors. Thanks. Any idea why I was receiving the error even though it seemed to be working?
Do you mean with your original script :?:

Usually I use the return of the command rather than the text message in case the author decides to change it.

Normally if a command succeeds it returns 0 otherwise it will return a non 0 value.

Code: Select all

[bill@XPS] ~/script $ mountpoint /mnt;echo $?
/mnt is not a mountpoint
1
[bill@XPS] ~/script $ mountpoint /;echo $?
/ is a mountpoint
0
[bill@XPS] ~/script $ expressvpn;echo $?
expressvpn: command not found
127
[bill@XPS] ~/script $ 
However, expressvpn appears to return a 0 exit status regardless if it's connected or not so checking the return status with $? will not work in this case.
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

Re: [Solved] IF THEN to check expressvpn status

Post by Sugarcrisp »

Thanks for the help WharfRat :D
Locked

Return to “Scripts & Bash”