[Solved] IF THEN to check expressvpn status

Forum rules
Before you post please read how to get help
Post Reply
Sugarcrisp
Level 1
Level 1
Posts: 37
Joined: Sat Mar 31, 2018 6:58 am

[Solved] IF THEN to check expressvpn status

Post by Sugarcrisp » Thu Apr 05, 2018 9:09 am

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 Sugarcrisp on Thu Apr 05, 2018 11:43 am, edited 1 time in total.

User avatar
WharfRat
Level 20
Level 20
Posts: 11585
Joined: Thu Apr 07, 2011 8:15 pm

Re: IF THEN to check expressvpn status

Post by WharfRat » Thu Apr 05, 2018 9:14 am

What do you get in the terminal when you enter expressvpn status then echo $?
ImageImage

Sugarcrisp
Level 1
Level 1
Posts: 37
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp » Thu Apr 05, 2018 9:42 am

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

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

User avatar
WharfRat
Level 20
Level 20
Posts: 11585
Joined: Thu Apr 07, 2011 8:15 pm

Re: IF THEN to check expressvpn status

Post by WharfRat » Thu Apr 05, 2018 10:04 am

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 $?
ImageImage

Sugarcrisp
Level 1
Level 1
Posts: 37
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp » Thu Apr 05, 2018 10:29 am

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.

User avatar
WharfRat
Level 20
Level 20
Posts: 11585
Joined: Thu Apr 07, 2011 8:15 pm

Re: IF THEN to check expressvpn status

Post by WharfRat » Thu Apr 05, 2018 10:34 am

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
ImageImage

blockhead47
Level 2
Level 2
Posts: 80
Joined: Wed Jun 15, 2016 4:50 pm
Location: Chicago, IL, USA

Re: IF THEN to check expressvpn status

Post by blockhead47 » Thu Apr 05, 2018 11:11 am

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 1
Level 1
Posts: 37
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp » Thu Apr 05, 2018 11:13 am

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

User avatar
WharfRat
Level 20
Level 20
Posts: 11585
Joined: Thu Apr 07, 2011 8:15 pm

Re: IF THEN to check expressvpn status

Post by WharfRat » Thu Apr 05, 2018 11:26 am

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 $?
ImageImage

Sugarcrisp
Level 1
Level 1
Posts: 37
Joined: Sat Mar 31, 2018 6:58 am

Re:IF THEN to check expressvpn status

Post by Sugarcrisp » 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?
Last edited by Sugarcrisp on Thu Apr 05, 2018 11:44 am, edited 1 time in total.

Sugarcrisp
Level 1
Level 1
Posts: 37
Joined: Sat Mar 31, 2018 6:58 am

Re: IF THEN to check expressvpn status

Post by Sugarcrisp » Thu Apr 05, 2018 11:43 am

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

User avatar
WharfRat
Level 20
Level 20
Posts: 11585
Joined: Thu Apr 07, 2011 8:15 pm

Re: Re:IF THEN to check expressvpn status

Post by WharfRat » Thu Apr 05, 2018 11:54 am

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.
ImageImage

Sugarcrisp
Level 1
Level 1
Posts: 37
Joined: Sat Mar 31, 2018 6:58 am

Re: [Solved] IF THEN to check expressvpn status

Post by Sugarcrisp » Thu Apr 05, 2018 12:43 pm

Thanks for the help WharfRat :D

Post Reply

Return to “Scripts & Bash”