• Skip to primary navigation
  • Skip to main content

Collin M. Barrett

Collin M. Barrett is a staff software engineer in Memphis, TN.

  • Start
  • Blog
  • Resume
    • Projects
    • References
  • Contact
Home ❭ InfoSec ❭ Automatic VPN on Android with Tasker and OpenVPN
Automatic VPN on Android with Tasker and OpenVPNPhoto by Kamil & Paweł Malinowski

Automatic VPN on Android with Tasker and OpenVPN

Published: 1.17.16

In the Barrett house, our wireless router is running DD-WRT with the OpenVPN client to a 3rd-party VPN provider always enabled. To hinder tracking by ISPs and the NSA, I try always to connect to a VPN on Android. When I enable it while connected to my home network, however, I notice a performance hit because I am recursively tunneling through a tunnel already being created by our wireless router. Using Tasker (root is not required) in conjunction with OpenVPN Connect, I have automated the connection and disconnection of the VPN service on my Android device. However, I do not take the extra performance overhead of the second connection when I am at home. Here is how you can automatically connect to VPN on Android using this same strategy.

Assumptions

You must have a subscription to an OpenVPN-compatible service. You should configure OpenVPN Connect on your Android phone per the instructions provided by your VPN service. You must also purchase and install Tasker before continuing.

I am not sure why, but it seems that Tasker must also have the Location Android permission enabled for the profiles to be triggered correctly.

Tasker Tasks

Configure the following two tasks with one action each in Tasker. 1Note: Items in curly brackets {} should be treated as variables and replaced.

“VPN Connect” Task

Add action type “System”->“Send Intent” and configure as follows.

  • Action: net.openvpn.openvpn.CONNECT
  • Cat: None
  • Mime Type: {blank}
  • Data: {blank}
  • Extra: net.openvpn.openvpn.AUTOSTART_PROFILE_NAME:PC {your_profile_name}
  • Extra: net.openvpn.openvpn.AUTOCONNECT:true
  • Extra: net.openvpn.openvpn.APP_SECTION:PC
  • Package: net.openvpn.openvpn
  • Class: net.openvpn.unified.MainActivity
  • Target: Activity
  • Continue Task After Error: {unchecked}
  • If: {blank}
  • Label: {blank}

“VPN Disconnect” Task

Add action type “System”->“Send Intent” and configure as follows.

  • Action: net.openvpn.openvpn.DISCONNECT
  • Cat: None
  • Mime Type: {blank}
  • Data: {blank}
  • Extra: net.openvpn.openvpn.STOP:true
  • Extra: {blank}
  • Extra: {blank}
  • Package: net.openvpn.openvpn
  • Class: net.openvpn.unified.MainActivity
  • Target: Activity
  • Continue Task After Error: {unchecked}
  • If: {blank}
  • Label: {blank}

Tasker Profiles

Configure the following two profiles in Tasker and link them to the task specified.

“Wi-Fi Disconnected” State Profile

Add profile and set as follows. Link this profile to the “VPN Connect” task.

  • SSID: {blank}
  • MAC: {blank}
  • IP: {blank}
  • Active: Yes
  • Inverted: {checked}

“Wi-Fi Connected” State Profile

Add profile and configure as follows. Link this profile to the “VPN Disconnect” task.

  • SSID: {blank}
  • MAC: {blank}
  • IP: {blank}
  • Active: Yes
  • Inverted: {unchecked}

via levgen

Related

About Collin M. Barrett

Collin M. Barrett is a staff software engineer in Memphis, TN.

Subscribe! (max one per week)

Reader Interactions

Comments

  1. martin flaherty says

    February 23, 2016 at 3:54 am

    Hi.

    {myOpenVpnProfileName} is this an ovpn file and where is it located.. I get openvpn profile not found error in the OpenVPN connect app

    Reply
    • Collin M. Barrett says

      February 23, 2016 at 6:02 am

      @Martin, I should have clarified that a bit better. You first have to import the opvn profile into the OpenVPN app on Android as you normally would from the settings menu. Once you’ve got the profile imported, verify you can connect from within OpenVPN by tapping “connect”. Then, in Tasker, {myOpenVpnProfileName} refers to the name (either default or customized when you imported the profile) of the profile in OpenVPN, no directory/location information needed. Let me know if I can help clarify further.

  2. Jared says

    July 11, 2016 at 3:37 pm

    Tried your walkthrough but having a bit of troubles.
    – My Tasker version is 4.8u5
    – My OpenVPN for Android is 0.6.57
    – My device is a Samsung Tab A, that is rooted.
    Both downloaded off of the Google Playstore July 11, 2016.

    OpenVPN for Android I imported my .ovpn file and can manually connect / disconnect my VPN just fine.

    I have entered everything verbatim (aside variables as you specified) with the exception of:
    •Continue Task After Error:
    •Label: VPN Connect

    Both the “VPN Connect” Task and “VPN Disconnect” Task do not have these settings to populate. Not sure if this would adversely affect operation.

    But when I go to test each task by manually executing them in the Tasker app, the OpenVPN doesn’t start, nor if I manually start my openvpn will the Disconnect task end openvpn operation. The 5seconds of wait time does work.

    Is there anything that may have changed since this writing back in January 2016?

    Reply
    • Jared says

      July 11, 2016 at 4:27 pm

      Also tried this on my non rooted Samsung S5, same versions for OpenVPN and Tasker.

    • Collin M. Barrett says

      July 12, 2016 at 9:00 am

      Hi @Jared,

      It is quite possible that something has changed. I do not currently use this system (I just leave OpenVPN always connected, not using Tasker). I do have on my to-do list to do a revised write-up of this process, but I am not sure when I will have time to do that. Sorry, I can’t think of anything to offer to you to try until I dig in and retry on my own setup, which I don’t have time for in the near future. Good luck! Check back in awhile (few months) for a possible update to the post.

  3. chuck says

    July 26, 2016 at 4:19 am

    Working fine here, using the same settings you specified except with a similar app called Macrodroid

    Reply
    • Collin M. Barrett says

      July 26, 2016 at 5:50 am

      @chuck Great to hear! Thanks for the feedback!

  4. Shaggy says

    November 3, 2016 at 4:58 am

    Works great on my Google Pixel XL! Thanks for the code!

    Reply
    • Collin M. Barrett says

      November 3, 2016 at 8:56 am

      Thanks! Glad to hear it!

  5. steve says

    February 7, 2017 at 1:46 pm

    This is working on my Pixel after connecting Private Internet Access with OpenVPN. However, the “VPN Connect” Task causes the OpenVPN appear to appear briefly on screen; is there a way to prevent this? Also, from within the PIA app itself, there are settings for MACE ad blocking, is that available from OpenVPN this way as well?

    Reply
    • Collin M. Barrett says

      February 7, 2017 at 1:52 pm

      Hi Steve,

      I do not know of a way to prevent briefly showing the OpenVPN app on connect. However, I have been using this system for months and I hardly ever notice it. I am using a Nexus 6P, and the connect almost always happens when my screen is off as I am driving away from my home. So, it’s not really an issue for my usage.

      The ad-blocking feature sounds like a custom feature for PIA. Adblocking really works best at the browser level, anyway. Blocking at the network level (like via a VPN) can only block entire domains but can’t effect stuff inside of https tunnels. If you want a great adblocking solution on Android, I recommend the Firefox browser with uBlock Origin and some of the lists I have (shameless plug for my side project) over at https://filterlists.com

      Hope that helps!

  6. tyrone Murray says

    March 7, 2017 at 3:53 pm

    Am new to all this but can this be done on Android with another vpn app? let me share my problem,I have a vpn that doesn’t connect on the first try so it attempt to connect two more times automatically,I would want to add more automatic re-connecting tries. am rooted xposed installed so any instructions would be nice. [email protected]

    Reply
    • Collin M. Barrett says

      March 7, 2017 at 6:43 pm

      Hi Tyrone, The method described in this post is strictly for the official OpenVPN app. That being said, most good VPN services support OpenVPN. Even if the service you use has their own app, you might be able to connect to it via the OpenVPN app as well. I cannot provide specific instructions for that, though. You will need to contact your VPN provider. Hope that helps.

  7. M says

    June 24, 2017 at 6:09 am

    Thanks a million times!

    Reply
    • Collin M. Barrett says

      June 29, 2017 at 1:14 pm

      Glad it helped!

  8. Brian says

    June 29, 2017 at 1:08 pm

    Or an easier method would be to have a reserved ip for your phone and not send it’s traffic through the VPN on the router.

    Reply
    • Collin M. Barrett says

      June 29, 2017 at 1:12 pm

      Hi Brian, That’s a great idea! It may not be supported on all router / router OSs, though.

  9. Sam says

    September 26, 2017 at 2:45 pm

    This approach does not work in the latest version of OpenVPN for Android. The developer disabled the use of intents to connect and disconnect the VPN. Instead there is a openvpn tasker plugin(found on play store) now that can control openvpn for android.

    Reply
    • Collin M. Barrett says

      September 26, 2017 at 2:55 pm

      Hi Sam,

      Thanks. This article is targeting OpenVPN Connect, however, which still supports intents as described. OpenVPN for Android is a different app which this article does not claim to support.

  10. John says

    November 2, 2017 at 9:09 pm

    Thank you so much for this. Its great.

    When I implemented this, I noticed a couple of minor things about the profiles. The wifi connected profile should have the invert box Unchecked, and the wifi disconnected should have it checked, your instructions had them reversed. Also, on mine, the wifi connected profile also had to have the Active parameter set to “Any”. This was because while the VPN is connected, it wasn’t detecting that I reconnected to Wifi.

    Reply
    • Collin M. Barrett says

      November 3, 2017 at 8:27 pm

      Hi John, Thanks for the tips. You are correct! I just updated the piece.

  11. Jesse says

    November 4, 2017 at 1:23 pm

    I can’t thank you enough for this tutorial. I made the decision to move from a Nexus phone to the Moto X4 and I am sincerely missing root (there is not yet a safe way to do this with the X4). I should have just bought a Pixel.

    This made me turn to an alternative method for ad-blocking – AB-Solution on my ASUS router at home. I found myself needing to set up my own VPN on the ASUS so I could remain connected to my home network while away. While it works fine, it was not automated. Tasker was the obvious solution, but I was concerned it would not function properly without root.

    Your instructions worked wonderfully. The only difference being the “Continue After Error” and “Label” options when creating tasks were not available. Labeling the task was available for me, however, through another menu with Tasker.

    Device: Moto X4 Android One Edition
    Android Version: 7.1.1
    Tasker Version: 5.0u7
    OpenVPN Connect Version: 1.1.23

    Reply
  12. Julio says

    November 15, 2017 at 12:15 pm

    Hi, this is great!!!

    Is it possible to connect vpn whenever on wifi and disconnect if in cellular data?
    I want to use vpn even at home when connected through wifi.

    Tanks!

    Reply
    • Collin M. Barrett says

      November 15, 2017 at 6:00 pm

      Hi Julio,

      In this case, I just wouldn’t use Tasker at all and keep the VPN connected all the time (whether on cellular or WiFi).

  13. Jeremy says

    December 9, 2017 at 11:21 am

    Very good. Thanks for the helpful information. As an extra step, I also configured a profile to connect to my VPN whenever I’m on a cellular network. As a previous post indicated, I also use an Asus router with ab-solutions installed so I always want the benefits that has to offer to my mobile devices.

    Cheers.

    Reply
    • Collin M. Barrett says

      December 30, 2017 at 5:06 pm

      Sounds like a winning setup. Thanks for sharing!

  14. Rob van Brunschot says

    January 5, 2018 at 9:09 am

    Just what I needed to Connect or Disconnect to Open VPN when I want to via Tasker.
    Thank you for sharing.
    Regards Rob

    Reply
    • Collin M. Barrett says

      January 5, 2018 at 9:13 am

      Hi Rob, glad it helped! Let me know if you had any issues with it. I am working on an updated version of this post since it seems to be a popular one that has helped quite a few folks.

  15. ToniCipriani says

    January 10, 2018 at 10:13 am

    I tried this, but use it a bit differently, only want the VPN to connect when on a specific WiFi. However when the WiFi connects, I see OpenVPN connects, but then immediately disconnect?

    Reply
    • Collin M. Barrett says

      January 10, 2018 at 10:16 am

      Hi Toni,

      I like that specific use case and am actually doing something very similar myself currently. I’m working on a re-write of this article, so check back soon.

  16. ToniCipriani says

    January 10, 2018 at 4:28 pm

    Turns out it was my fault, I missed setting the Wi-Fi “Any” setting.

    Also, it seems possible to merge the two profiles by adding the connect task as an exit condition when you leave the home profile (in my case the disconnect task)?

    Reply
    • Collin M. Barrett says

      January 11, 2018 at 5:13 am

      Ah, ok, glad to hear you figured it out.

      Hmm, I’ll give that suggestion a shot. You might be right.

  17. Greg says

    January 11, 2018 at 11:06 am

    Thank you for this. I configured exactly as you outlined, but my profile is not connecting. When my home WiFi disconnects the openVPN Connect app opens, but my profile does not load. I double-checked my spelling and syntax. Please help!

    Reply
    • Collin M. Barrett says

      January 12, 2018 at 7:53 am

      Sorry, Greg. I am not sure to suggest. I am working on testing this method out fully again and re-writing this article with more options and details. Maybe check back soon and try again if I get that finished. Good luck!

  18. Rolando says

    January 16, 2018 at 11:33 pm

    hi! thax for this, one question there´s a way to use with Datally (Google´s App)? for block apps to use mobile data?

    Reply
    • Collin M. Barrett says

      January 17, 2018 at 5:51 am

      Since Datally sets up a local VPN server, using the Android’s VPN feature, I do not think there is a way to also use an actual OpenVPN client as well. Sorry.

  19. Rolando says

    January 17, 2018 at 7:33 am

    Thanx for your reply maybe I’ll try openVPN

    Reply
  20. raimund says

    February 14, 2018 at 2:57 pm

    Thx Collin,
    worked for me for two years.
    But unfortunally today came an update for the OpenVPNconnect to version 3.0.0 and now this solution isn’t working anymore.
    Maybe you can check the settings for this new version?

    thx
    raimund

    Reply
    • Collin M. Barrett says

      February 15, 2018 at 8:38 am

      Thanks for the info. I just added a notice to the top of the article. I am unaware of any new solution for the new version, but please let us know here if you find one. Sorry.

  21. ToniCipriani says

    February 15, 2018 at 8:26 am

    I think the newest OpenVPN update broke this, the tasks no longer trigger the connection.

    Reply
    • Collin M. Barrett says

      February 15, 2018 at 8:38 am

      Thanks for the info. I just added a notice to the top of the article. I am unaware of any new solution for the new version, but please let us know here if you find one. Sorry.

  22. Bryan Green says

    February 15, 2018 at 9:30 am

    I sent an email to the developer asking if there was documentation on intents available in OpenVPN Connect 3.0.

    We’ll see what kind of response I get.

    Reply
  23. Julio C. Villasante says

    February 15, 2018 at 11:52 am

    I also wrote the developer linking this page as reference. Hopefully he can be able to help.

    Reply
  24. Ievgen says

    February 16, 2018 at 6:31 am

    Please use following options for new version of OpenVPN Connect application:

    [u][b]1. CONNECT[/b][/u]

    [b][i]a) Access Server module[/i][/b]
    Action: net.openvpn.openvpn.CONNECT
    [b][i]OR[/i][/b]
    Action: android.intent.action.VIEW
    Cat: None
    Mime Type: {blank}
    Data: {blank}
    Extra: net.openvpn.openvpn.AUTOSTART_PROFILE_NAME:AS {your_profile_name}
    Extra: net.openvpn.openvpn.AUTOCONNECT:true
    Extra: net.openvpn.openvpn.APP_SECTION:AS
    Package: net.openvpn.openvpn
    Class: net.openvpn.unified.MainActivity
    Target: Activity

    [b][i]
    b) OVPN Profile module[/i][/b]
    Action: net.openvpn.openvpn.CONNECT
    [i][b]OR[/b][/i]
    Action: android.intent.action.VIEW
    Cat: None
    Mime Type: {blank}
    Data: {blank}
    Extra: net.openvpn.openvpn.AUTOSTART_PROFILE_NAME:PC {your_profile_name}
    Extra: net.openvpn.openvpn.AUTOCONNECT:true
    Extra: net.openvpn.openvpn.APP_SECTION:PC
    Package: net.openvpn.openvpn
    Class: net.openvpn.unified.MainActivity
    Target: Activity

    [b][i]
    2. DISCONNECT[/i][/b]
    Action: net.openvpn.openvpn.DISCONNECT
    Cat: None
    Mime Type: {blank}
    Data: {blank}
    Extra: net.openvpn.openvpn.STOP:true
    Extra: {blank}
    Extra: {blank}
    Package: net.openvpn.openvpn
    Class: net.openvpn.unified.MainActivity
    Target: Activity

    Reply
    • raimund says

      February 16, 2018 at 7:13 am

      Great work!
      Thank you for this quick solution!!!!

      rai

  25. secpro says

    February 16, 2018 at 5:47 pm

    The OVPN profile module solution works like a charm for me. @levgen Thank you very much.. you are a life saver!

    Reply
  26. andy says

    February 18, 2018 at 12:28 pm

    Hi,

    i try to adopt levgen’s answer to Automate (tasker-alternative), but can’t get it work. OpenVPN Connect starts, but do nothing (doesnt change to OVPN Profile or anything else).

    Disconnect seems to work, but i’m wondering, why the switch stays at a circle.

    By the way: I use an OVPN profile.

    Any ideas?

    Andy

    Reply
    • Ievgen says

      February 19, 2018 at 6:31 am

      Hi Andy,
      I never used Automate tool. Could you provide options which you used for previous version of OpenVPN Connect application. After that I will back soon with related data.

  27. Chris Browning says

    February 18, 2018 at 6:11 pm

    It looks like the most recent upgrade to openvpn broke this. It was working for me and then when openvpn upgraded, it stopped working.

    Reply
  28. Chris Browning says

    February 18, 2018 at 6:53 pm

    I guess it would help if i read the new comments!

    Reply
    • Chris Browning says

      February 18, 2018 at 7:03 pm

      Forgot to mention that levgen’s solution works for me too. Thanks!

  29. Henry says

    March 24, 2018 at 9:29 am

    in ios version of openvpn app i am able to define connection via cellular data only or by wifi only or both with simple option “connect via” in app settings.
    thus there is no need for tasker bloatware, tasker is slow and allows data leak during switching.
    was it to help tasker sales or help track us by these 3 letters organizations?
    why openvpn consortium created such mess for android users?

    Reply
    • Collin M. Barrett says

      April 13, 2018 at 10:19 am

      Huh, interesting point. I’m not familiar with how the iOS version works. I’d be happy to not have to mess with Tasker to get this to work on Android.

  30. jerome says

    April 20, 2018 at 8:49 pm

    OK, doesn’t work for me, I just get a white screen

    Reply
  31. Leo Nesfield says

    August 11, 2018 at 4:45 pm

    Hi Collin, I’m trying this on my Nexus 6P, and this is more of a Tasker issue but when ever I connect to my home WiFi, it does not recognise that and still says “Not Wifi Connected” in green.

    Reply
    • Leo Nesfield says

      August 11, 2018 at 4:49 pm

      Ah, I found the answer on reddit, https://www.reddit.com/r/tasker/comments/6ovz03/tasker_not_recognizing_wifi_when_vpn_connected/

      I need to select “any” on the Wifi page

    • Collin M. Barrett says

      February 27, 2019 at 7:40 pm

      Ah, thanks for sharing.

  32. Mike Casper says

    October 6, 2018 at 6:05 am

    Thanks for posting this. I have it all working except – it takes a few minutes for vpn to finally disconnect. Not sure why. Any ideas?

    Reply
    • Collin M. Barrett says

      February 27, 2019 at 7:39 pm

      Hmm, really not sure. Sorry. I honestly gave up on trying to keep this automation working because it was so fragile (connect/disconnect at random times or not at all when I had hoped). I hope to re-visit at some point and will update this post if I do.

  33. Kaarel says

    January 13, 2020 at 4:37 pm

    Do you know what I need to add in order to add a password?
    My latest openvpn setup requires a password every time I connect (it isn’t possible to save it). So now when with your method tasker sends a connect to vpn a popup will show with an input field for the password.
    Can the password be added in tasker somewhere?

    Reply
    • Collin M. Barrett says

      January 14, 2020 at 6:23 pm

      Hmm, I’m never had to cache a password before. I’m not sure. Sorry. Good luck. Let us know if you figure it out!

  34. Chera Bekker says

    April 19, 2020 at 1:07 pm

    This setup still works on Android 9. I did however had to introduce an extra wait for 5 s to prevent a too early startup of openvpn before the 4G connection had time to settle.

    Reply
    • Chera Bekker says

      April 19, 2020 at 3:56 pm

      actually i reduced the wait down to 1 s.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Collin M. Barrett © 2023

  • LinkedIn
  • GitHub
  • Stack Overflow
  • DEV
  • Twitter
  • Goodreads
  • Sitemap