[QUESTION] [GUIDE] VPN always active [DISCUSSION]



  • Introduction

    Hi, I successfully configured server and client on UT with rpi, pihole and pivpn on it.

    When cellular data connection is poor and it's being interrupted, vpn disconnects and I have to click its switch so many times.

    Problem

    When cellular data connection is poor and it's being interrupted, vpn disconnects and I have to click its switch so many times...

    Question

    What is the nmcli command to enable cellular data connection?

    Solution

    based on this askubuntu thread

    Step 1:
    open terminal on your phone or use ssh as you wish 🙂

    (I replaced UUID with "MY_ID" and my vpn name with "MY_VPN_NAME")

    phablet@ubuntu-phablet:~$ nmcli c show --active | grep vpn
    MY_VPN_NAME             MY_ID  vpn              ril_0  
    

    Step 2:

    phablet@ubuntu-phablet:~$ nmcli c edit MY_ID
    
    ===| nmcli interactive connection editor |===
    
    Editing existing 'vpn' connection: 'MY_ID'
                                                
    Type 'help' or '?' for available commands.  
    Type 'describe [<setting>.<prop>]' for detailed property description.                   
    
    You may edit the following settings: connection, vpn, ipv4, ipv6
    

    Step 3:

    nmcli> print connection.autoconnect
    connection.autoconnect: no
    

    Step 4:

    nmcli> set connection.autoconnect yes
    

    Step 5

    nmcli> save persistent
    Saving the connection with 'autoconnect=yes'. That might result in an immediate activation of the connection.
    Do you still want to save? (yes/no) [yes] yes
    Connection 'MY_VPN_NAME' (MY_ID) successfully updated.
    
    nmcli> 
    
    

    Step 6:
    (based on this askubuntu thread)

    nmcli> print vpn.persistent
    vpn.persistent: no
    

    Step 7:

    nmcli> set vpn.persistent yes
    

    Step 8:

    nmcli> save persistent
    Connection 'MY_VPN_NAME' (MY_ID) successfully updated.
    

    Last thing:
    Save one of needed script in home direction and make it executable, replace only: MY_ID and MY_VPN_NAME
    replace "2s" with any value if you want the script to run at different intervals
    You can also add it to autostart.

    based on this

    To run any of this script you have to use command . /home/phablet/path/to/your/script.sh not sh /home/phablet/path/to/your/script.sh because . is not exactly equal to sh

    1. Suggested namevar-dnmice_0.0.1.sh (Shortcut of: VPN auto reconnect, does not mind if connection is enabled 😉 ) This script below enables vpn connection and checks if you connected to internet if not it doesn't mind and go through.
    #!/bin/bash +x
    #__      _______  _   _                 _                                                   _                     _       _
    #\ \    / /  __ \| \ | |               | |                                                 | |                   (_)     | |
    # \ \  / /| |__) |  \| |     __ _ _   _| |_ ___    _ __ ___  ___ ___  _ __  _ __   ___  ___| |_     ___  ___ _ __ _ _ __ | |_
    #  \ \/ / |  ___/| . ` |    / _` | | | | __/ _ \  | '__/ _ \/ __/ _ \| '_ \| '_ \ / _ \/ __| __|   / __|/ __| '__| | '_ \| __|
    #   \  /  | |    | |\  |   | (_| | |_| | || (_) | | | |  __/ (_| (_) | | | | | | |  __/ (__| |_    \__ \ (__| |  | | |_) | |_
    #    \/   |_|    |_| \_|    \__,_|\__,_|\__\___/  |_|  \___|\___\___/|_| |_|_| |_|\___|\___|\__|   |___/\___|_|  |_| .__/ \__|
    #                                                                                                                  | |
    #                                                                                                                  |_|
    #
    # ============================================================================================================================
    # ============================================================================================================================
    # ===================================================> IMPORTANT <============================================================
    # =========> YOU HAVE TO REPLACE ALL "MY_ID" WITH YOUR VPN UUID AND "MY_VPN_NAME" WITH SAVED IN SYSTEM VPN NAME. <============
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ===========> If you will remeber which script is for what, you can comment/delete below 22-27 lines. <======================
    # ============================================================================================================================
    
    clear
    echo "Welcome, to VPN auto reconnect script :) "
    echo ""
    echo "This script is set to reconnect to set VPN in specified time (default is 2sec). It doesn't mind if cellular data or wifi is enabled, just goes through. - It's good, when your signal is poor."
    sleep 9s
    clear
    
    echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    while [ "true" ]
    do
            VPNCON=$(nmcli connection show --active| grep MY_VPN_NAME | cut -f1 -d " ")
            NETWORK=$(nmcli -f STATE -t g )
    
    
            if [[ $VPNCON == MY_VPN_NAME ]]; then
                    echo "Already connected to ''MY_VPN_NAME'' VPN"
            elif [[ $VPNCON != MY_VPN_NAME && $NETWORK == connected ]]; then
                    echo "Disconnected from ''MY_VPN_NAME'' VPN but connected to the Network, trying to reconnect..."
                    (sleep 1s && nmcli con up uuid MY_ID)
    
            fi
    #Below - sleep 2 is interval of checking vpn status, change for value you want :)
            sleep 2s
            clear
    	echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    done
    
    1. Suggested namevar-ewnic_0.0.1.sh (VPN auto reconnect, exit when no Internet connection) This script enables vpn when you have internet connection but stop when you disable internet connection
    #!/bin/bash +x
    #__      _______  _   _                 _                                                   _                     _       _
    #\ \    / /  __ \| \ | |               | |                                                 | |                   (_)     | |
    # \ \  / /| |__) |  \| |     __ _ _   _| |_ ___    _ __ ___  ___ ___  _ __  _ __   ___  ___| |_     ___  ___ _ __ _ _ __ | |_
    #  \ \/ / |  ___/| . ` |    / _` | | | | __/ _ \  | '__/ _ \/ __/ _ \| '_ \| '_ \ / _ \/ __| __|   / __|/ __| '__| | '_ \| __|
    #   \  /  | |    | |\  |   | (_| | |_| | || (_) | | | |  __/ (_| (_) | | | | | | |  __/ (__| |_    \__ \ (__| |  | | |_) | |_
    #    \/   |_|    |_| \_|    \__,_|\__,_|\__\___/  |_|  \___|\___\___/|_| |_|_| |_|\___|\___|\__|   |___/\___|_|  |_| .__/ \__|
    #                                                                                                                  | |
    #                                                                                                                  |_|
    #
    # ============================================================================================================================
    # ============================================================================================================================
    # ===================================================> IMPORTANT <============================================================
    # =========> YOU HAVE TO REPLACE ALL "MY_ID" WITH YOUR VPN UUID AND "MY_VPN_NAME" WITH SAVED IN SYSTEM VPN NAME. <============
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ===========> If you will remeber which script is for what, you can comment/delete below 22-27 lines. <======================
    # ============================================================================================================================
    
    clear
    echo "Welcome, to VPN auto reconnect script :) "
    echo ""
    echo "This script is set to reconnect to set VPN in specified time (default is 2sec) when you have connection to the Internet. If not, It'll exit"
    sleep 6s
    clear
    
    echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    while [ "true" ]
    do
            VPNCON=$(nmcli connection show --active| grep MY_VPN_NAME | cut -f1 -d " ")
            NETWORK=$(nmcli -f STATE -t g )
    
    
            if [[ $VPNCON == MY_VPN_NAME ]]; then
                    echo "Already connected to ''MY_VPN_NAME'' VPN"
            elif [[ $VPNCON != MY_VPN_NAME && $NETWORK == connected ]]; then
                    echo "Disconnected from ''MY_VPN_NAME'' VPN but connected to the Network, trying to reconnect..."
                    (sleep 1s && nmcli con up uuid MY_ID)
    
            elif [[ $NETWORK == disconnected ]]; then
    		clear
             	echo "Device is offline, this window will disappear in 3 seconds"
             	sleep 3s
          		exit
            fi
    #Below - sleep 2 is interval of checking vpn status, change for value you want :)
            sleep 2s
            clear
    	echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    done
    
    1. Suggested namevar-ewoncac_0.0.1.sh (VPN auto reconnect, enables wifi on no connection and continues) This script enables vpn and checks if you have internet connection, when you are offline it tries to enable wifi and continue tries to establish connection with vpn.
    #!/bin/bash +x
    #__      _______  _   _                 _                                                   _                     _       _
    #\ \    / /  __ \| \ | |               | |                                                 | |                   (_)     | |
    # \ \  / /| |__) |  \| |     __ _ _   _| |_ ___    _ __ ___  ___ ___  _ __  _ __   ___  ___| |_     ___  ___ _ __ _ _ __ | |_
    #  \ \/ / |  ___/| . ` |    / _` | | | | __/ _ \  | '__/ _ \/ __/ _ \| '_ \| '_ \ / _ \/ __| __|   / __|/ __| '__| | '_ \| __|
    #   \  /  | |    | |\  |   | (_| | |_| | || (_) | | | |  __/ (_| (_) | | | | | | |  __/ (__| |_    \__ \ (__| |  | | |_) | |_
    #    \/   |_|    |_| \_|    \__,_|\__,_|\__\___/  |_|  \___|\___\___/|_| |_|_| |_|\___|\___|\__|   |___/\___|_|  |_| .__/ \__|
    #                                                                                                                  | |
    #                                                                                                                  |_|
    #
    # ============================================================================================================================
    # ============================================================================================================================
    # ===================================================> IMPORTANT <============================================================
    # =========> YOU HAVE TO REPLACE ALL "MY_ID" WITH YOUR VPN UUID AND "MY_VPN_NAME" WITH SAVED IN SYSTEM VPN NAME. <============
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ===========> If you will remeber which script is for what, you can comment/delete below 22-27 lines. <======================
    # ============================================================================================================================
    
    clear
    echo "Welcome, to VPN auto reconnect script :) "
    echo ""
    echo "This script is set to reconnect to set VPN in specified time (default is 2sec). If you have WiFi set off, It'll be enabling it all the time and continues establishing VPN connection"
    sleep 9s
    clear
    
    echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    while [ "true" ]
    do
            VPNCON=$(nmcli connection show --active| grep MY_VPN_NAME | cut -f1 -d " ")
            NETWORK=$(nmcli -f STATE -t g )
    
    
            if [[ $VPNCON == MY_VPN_NAME ]]; then
                    echo "Already connected to ''MY_VPN_NAME'' VPN"
            elif [[ $VPNCON != MY_VPN_NAME && $NETWORK == connected ]]; then
                    echo "Disconnected from ''MY_VPN_NAME'' VPN but connected to the Network, trying to reconnect..."
                    (sleep 1s && nmcli con up uuid MY_ID)
    
            elif [[ $NETWORK == disconnected ]]; then
                            echo "Device offline, trying to enable WiFi"
                            nmcli r wifi on
                            sleep 2s
                            nmcli device wifi rescan
                            nmcli device set wlan0 autoconnect yes
                            clear
                            nmcli d
                            echo""
                            echo""
                            nmcli device wifi list
                           sleep 2s
    
    
            fi
    #Below - sleep 2 is interval of checking vpn status, change for value you want :)
            sleep 2s
            clear
    	echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    done
    
    1. Suggested namevar-ewonceadc_0.0.1.sh (Shortcut of: VPN auto reconnect, enables wifi on no connection, exit after double check) This script retries to enable vpn, check if you are connected to network if not enables wifi and waits. If you still doesn't have connection, it exits.
    #!/bin/bash +x
    #__      _______  _   _                 _                                                   _                     _       _
    #\ \    / /  __ \| \ | |               | |                                                 | |                   (_)     | |
    # \ \  / /| |__) |  \| |     __ _ _   _| |_ ___    _ __ ___  ___ ___  _ __  _ __   ___  ___| |_     ___  ___ _ __ _ _ __ | |_
    #  \ \/ / |  ___/| . ` |    / _` | | | | __/ _ \  | '__/ _ \/ __/ _ \| '_ \| '_ \ / _ \/ __| __|   / __|/ __| '__| | '_ \| __|
    #   \  /  | |    | |\  |   | (_| | |_| | || (_) | | | |  __/ (_| (_) | | | | | | |  __/ (__| |_    \__ \ (__| |  | | |_) | |_
    #    \/   |_|    |_| \_|    \__,_|\__,_|\__\___/  |_|  \___|\___\___/|_| |_|_| |_|\___|\___|\__|   |___/\___|_|  |_| .__/ \__|
    #                                                                                                                  | |
    #                                                                                                                  |_|
    #
    # ============================================================================================================================
    # ============================================================================================================================
    # ===================================================> IMPORTANT <============================================================
    # =========> YOU HAVE TO REPLACE ALL "MY_ID" WITH YOUR VPN UUID AND "MY_VPN_NAME" WITH SAVED IN SYSTEM VPN NAME. <============
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ============================================================================================================================
    # ===========> If you will remeber which script is for what, you can comment/delete below 22-27 lines. <======================
    # ============================================================================================================================
    
    clear
    echo "Welcome, to VPN auto reconnect script :) "
    echo ""
    echo "This script is set to reconnect to set VPN in specified time (default is 2sec). If you have WiFi set off, It'll be enabling it all the time. If you'll have enabled wifi but you'll be somehow offline for specified time (default is 30sec) It'll exit"
    sleep 9s
    clear
    
    echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    while [ "true" ]
    do
            VPNCON=$(nmcli connection show --active| grep MY_VPN_NAME | cut -f1 -d " ")
            NETWORK=$(nmcli -f STATE -t g )
    
    
            if [[ $VPNCON == MY_VPN_NAME ]]; then
                    echo "Already connected to ''MY_VPN_NAME'' VPN"
            elif [[ $VPNCON != MY_VPN_NAME && $NETWORK == connected ]]; then
                    echo "Disconnected from ''MY_VPN_NAME'' VPN but connected to the Network, trying to reconnect..."
                    (sleep 1s && nmcli con up uuid MY_ID)
    
            elif [[ $NETWORK == disconnected ]]; then
                    echo "Device offline, trying to enable WiFi"
                    nmcli r wifi on
                    sleep 3s
                    nmcli device wifi rescan
                    nmcli device set wlan0 autoconnect yes
                    clear
                    nmcli d
                    echo""
                    echo""
                    nmcli device wifi list
    
                   echo ""
                   echo ""
    #Below - waiting for connection time, when device is offline, change for what you want :)
                   echo "I'll wait 30 seconds for connection"
                   sleep 30s
                   NETWORK1=$(nmcli -f STATE -t g )
             	case $NETWORK1 in
             		connected)
             			clear
             			echo "Successfully connected to WiFi"
             			sleep 3s
             			clear;;
             			
             		disconnected)
             			clear
             			echo "Device still offline, this window will disappear in 3 seconds"
             			sleep 3s
             			exit;;
    
             	esac
    
    
    
            fi
    #Below - sleep 2 is interval of checking vpn status, change for value you want :)
            sleep 2s
            clear
    	echo "To stop script press CTRL+C"
    echo ""
    echo ""
    echo "You Have configured VPNs in your system: "
    nmcli -c yes c show | grep vpn | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'm set to use below vpn"
    nmcli -c yes con show | grep MY_VPN_NAME | cut -f1 -d " "
    echo ""
    echo ""
    echo "I'll check if you are connected to internet"
    nmcli -c yes -f STATE -t g
    echo ""
    echo ""
    
    done
    

    Does it work for You? Report me 🙂

    Do You need something similar? - Try modifying by yourself or ask here

    You have idea connected with topic - write it here

    First Post changelog

    • 2020.08.20 - added one script, named others, updated all, since today when i edit script 0.0.0 numbers will change
    • 2020.08.19 - changed script which may be the solution if you run it with autostart
    • 2020.08.18 - added "my tests" header, added tag "openvpn"
    • 2020.08.10 - issue creation