Detection VPN connection ASUS Merlin Router  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by waaren »

Chris12 wrote: Thursday 06 May 2021 14:30 This looks like a missing library as well isn't it?
Don't know but seems anther missing link in the docker install. Please make sure you have Email and at least 1 other notification subsystem working.
Below script rely on these options.

Can you try this one

Code: Select all

local scriptVersion = '0.20210506_01'
local scriptVar  =  'SSH_' .. scriptVersion

--[[

This dzVents script is used to monitor open VPN state of a ASUS RT-AC86U router loaded with asuswrt-merlin firmware

The script use async executeShellCommand to trigger a nvram and when required do a service restart command on a remote system via ssh
(the router must be accessible by the user that is running the domoticz service, via password-less SSH (with public / private key setup)

Before activating the script:
    Read the GETTING STARTED section of the dzVents wiki.
    Change the values in the script to reflect your setup

requires:
    access to ssh and ping (extra actions needed to install these when in docker)
    passwordless access using ssh setup for the user executing domoticz
    domoticz >= V2020.2 12771 (dzVents >= 3.1.0)
    configured and working notification subsystem(s) and Email in domoticz


]]--

return
{
    on =
    {
        timer =
        {
            'every minute', -- change to required frequency
        },
        shellCommandResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = scriptVar,
    },

    execute = function(dz, item)

        local remoteHost = '192.168.1.1'   -- change to router IP
        local notConnectedMessage = 'OpenVPN not connected. Check it out'
        local reconnectedMessage = 'OpenVPN (re)connected'
        local wanDownMessage = 'Wan Connection down? Check it out'
        local remoteUser = 'Admin' -- domoticz uses user 'root' for executing commands, so the user 'root' should be password-less SSH access
        local remotePort = 2211
        local VPNStatus = dz.devices(1155)
        local notifyFrequency = 60 -- frequency in minutes

        -- =======================================================================
        --               NO changes required below this line
        -- =======================================================================

        --commands to execute remote
        local commands =
        {
            status = 'nvram get vpn_client1_state ',
            stop = 'service stop_vpnclient1 ',
            start = 'service start_vpnclient1 ',
            ping = 'ping -c1 -w1 8.8.8.8 ', -- ping command to check WAN connection available/down
        }
        commands.restart = commands.stop .. '; ' .. commands.start

        local function managedPopen(cmd)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
            dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_DEBUG)
            else -- all is fine!!
                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:' .. commandOutput, dz.LOG_DEBUG)
            end
            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

        local function osCommand(cmd, callback, timeout)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
            local timeout = timeout or 50

            dz.executeShellCommand(
            {
                command = cmd,
                callback = callback,
                timeout = timeout,
            })
        end

        local function buildSSHCommand(cmd)
            local sshBOL = "ssh " .. remoteUser .. "@" .. remoteHost .. " -p " .. remotePort .. " \'"
            local sshEOL = ";\'"
            local cmd = sshBOL .. cmd .. sshEOL
            return cmd
        end

        -- Main
        if item.isTimer then
            osCommand(buildSSHCommand(commands.status), scriptVar)
        else -- callback of the executeShellCommand

          --- dz.utils.dumpTable(item) -- Debug only
          if ( statusCode ~= 0 ) or ( item.data ~= 2 ) then
                if VPNStatus.state == 'On' or VPNStatus.lastUpdate.minutesAgo > notifyFrequency then
                    dz.log('Result: ' .. item.data .. ': ' .. notConnectedMessage, dz.LOG_ERROR)
                    dz.notify(scriptVar, notConnectedMessage, dz.PRIORITY_HIGH)
                    VPNStatus.switchOff().checkFirst()
                end
                osCommand(buildSSHCommand(commands.restart))
            else -- seems OK
                local result, rc = managedPopen(buildSSHCommand(commands.ping))
                if rc ~= 0 then
                    dz.log('VPN status is active but ping failed, internet WAN connection down?', dz.LOG_ERROR)
                    dz.notify(scriptVar, wanDownMessage, dz.PRIORITY_HIGH)
                elseif VPNStatus.state == 'Off' then
                    dz.log(reconnectedMessage, dz.LOG_DEBUG)
                    dz.notify(scriptVar, reconnectedMessage, dz.PRIORITY_LOW)
                    VPNStatus.switchOn().checkFirst()
                end
            end
        end
    end
}



Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by Chris12 »

@waaren, thanks for the new enhanced script.

When loading/running this script I can see some new error messages:

Code: Select all

2021-05-06 16:37:00.474 Error: Unable to read file /opt/domoticz/userdata/scripts/dzVents/data/domscript1.out
2021-05-06 16:37:00.474 Error: Unable to read file /opt/domoticz/userdata/scripts/dzVents/data/domscript1.err
which continue to generate these errors with an higher file number 2/3/4/5/6 etc.
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by Chris12 »

waaren wrote: Thursday 06 May 2021 16:21 Don't know but seems anther missing link in the docker install. Please make sure you have Email and at least 1 other notification subsystem working.
Below script rely on these options.
Email in domoticz (inside the docker) is working fine, I can receive test messages.
What do you mean with "at least 1 other notification subsystem" ? For now I only have email enabled.
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by waaren »

Chris12 wrote: Thursday 06 May 2021 20:29 What do you mean with "at least 1 other notification subsystem" ? For now I only have email enabled.
One of the lines in the script is

Code: Select all

dz.notify(scriptVar, notConnectedMessage, dz.PRIORITY_HIGH)
this does not make a lot of sense if you don't have any notification subsystems configured.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by waaren »

Chris12 wrote: Thursday 06 May 2021 16:42

Code: Select all

2021-05-06 16:37:00.474 Error: Unable to read file /opt/domoticz/userdata/scripts/dzVents/data/domscript1.out
2021-05-06 16:37:00.474 Error: Unable to read file /opt/domoticz/userdata/scripts/dzVents/data/domscript1.err
These are temp files generated by the executeShellCommand() Do you have this directory and is it accessible within the docker?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by Chris12 »

@waaren, I did not have the 'data' directory. Just added it and now those error message are gone!

The email error is still there: (only once when enabling/save the script)

Code: Select all

2021-05-06 20:59:01.254 Notification sent (email) => Failed
2021-05-06 20:59:01.253 Error: SMTP Mailer: Error sending Email to: <[email protected]> !
2021-05-06 20:59:01.253 Error: libcurl: (35)
2021-05-06 20:59:01.253 Error: gnutls_handshake() failed: Error in the pull function.
2021-05-06 20:59:01.253
2021-05-06 20:59:01.253 Error: Failed to send Email notification! 
Some thing I notice, is that I do not see the ping option anymore in the logfile when using this new script.
And it looks like the script is stopping/starting the vpn service everytime.

Code: Select all

2021-05-06 21:05:00.085 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW:, trigger: "every minute"
2021-05-06 21:05:00.098 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 21:05:00.098 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 21:05:00.098 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 21:05:00.098 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = SSH_0.20210506_01
2021-05-06 21:05:00.098 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-06 21:05:00.098 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-06 21:05:00.098 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW

2021-05-06 21:05:01.528 Status: dzVents: Info: Handling shellcommandResponse-events for: "SSH_0.20210506_01"
2021-05-06 21:05:01.528 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW: ShellCommandResponse: "SSH_0.20210506_01"
2021-05-06 21:05:01.541 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 21:05:01.541 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 21:05:01.541 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 21:05:01.541 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = nil
2021-05-06 21:05:01.541 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-06 21:05:01.541 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-06 21:05:01.541 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW 
the old script:

Code: Select all

2021-05-06 21:05:00.499 Status: dzVents: Info: SSH_0.20210503_01: ------ Start internal script: VPN Status:, trigger: "every minute"
2021-05-06 21:05:00.511 Status: dzVents: Debug: SSH_0.20210503_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 21:05:00.511 Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 21:05:00.814 Status: dzVents: Debug: SSH_0.20210503_01: ReturnCode: 0
2021-05-06 21:05:00.814 commandOutput:2
2021-05-06 21:05:00.814
2021-05-06 21:05:00.908 Status: dzVents: Debug: SSH_0.20210503_01: 2
2021-05-06 21:05:00.908
2021-05-06 21:05:00.908 Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'ping -c1 -w1 8.8.8.8 ;'
2021-05-06 21:05:01.211 Status: dzVents: Debug: SSH_0.20210503_01: ReturnCode: 0
2021-05-06 21:05:01.211 commandOutput:PING 8.8.8.8 (8.8.8.8): 56 data bytes
2021-05-06 21:05:01.211 64 bytes from 8.8.8.8: seq=0 ttl=117 time=7.511 ms
2021-05-06 21:05:01.211
2021-05-06 21:05:01.211 --- 8.8.8.8 ping statistics ---
2021-05-06 21:05:01.211 1 packets transmitted, 1 packets received, 0% packet loss
2021-05-06 21:05:01.211 round-trip min/avg/max = 7.511/7.511/7.511 ms
2021-05-06 21:05:01.211
2021-05-06 21:05:01.211 Status: dzVents: Info: SSH_0.20210503_01: ------ Finished VPN Status 
Does the new script not showing the ReturenCode and commandOutput variables?
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by waaren »

Chris12 wrote: Thursday 06 May 2021 21:11 The email error is still there: (only once when enabling/save the script)
No idea why this happens but would be surprised if it is specific to this script.
Some thing I notice, is that I do not see the ping option anymore in the logfile when using this new script.
And it looks like the script is stopping/starting the vpn service everytime.

Does the new script not showing the ReturenCode and commandOutput variables?
I guess the logic in the script does not interpret the result of the initial command.
change line

Code: Select all

--- dz.utils.dumpTable(item) -- Debug only
to

Code: Select all

 dz.utils.dumpTable(item) -- Debug only
to see what the returns are.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by Chris12 »

with the debug enabled:

Code: Select all

 2021-05-06 22:19:00.500 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW:, trigger: "every minute"
2021-05-06 22:19:00.512 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 22:19:00.512 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 22:19:00.512 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 22:19:00.512 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = SSH_0.20210506_01
2021-05-06 22:19:00.512 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-06 22:19:00.512 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-06 22:19:00.513 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW
2021-05-06 22:19:00.513 Status: EventSystem: Script event triggered: /opt/domoticz/dzVents/runtime/dzVents.lua
2021-05-06 22:19:00.775 Status: dzVents: Info: Handling shellcommandResponse-events for: "SSH_0.20210506_01"
2021-05-06 22:19:00.775 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW: ShellCommandResponse: "SSH_0.20210506_01"
2021-05-06 22:19:00.788 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 22:19:00.788 Status: dzVents: > dump()
2021-05-06 22:19:00.788 Status: dzVents: > statusText:
2021-05-06 22:19:00.788 Status: dzVents: > isTimer: false
2021-05-06 22:19:00.788 Status: dzVents: > isXML: false
2021-05-06 22:19:00.788 Status: dzVents: > data: 2
2021-05-06 22:19:00.788 Status: dzVents: > shellCommandResponse: SSH_0.20210506_01
2021-05-06 22:19:00.788 Status: dzVents: > isJSON: false
2021-05-06 22:19:00.788 Status: dzVents: > baseType: shellcommandResponse
2021-05-06 22:19:00.788 Status: dzVents: > isCustomEvent: false
2021-05-06 22:19:00.788 Status: dzVents: > isSecurity: false
2021-05-06 22:19:00.788 Status: dzVents: > errorText:
2021-05-06 22:19:00.788 Status: dzVents: > isVariable: false
2021-05-06 22:19:00.788 Status: dzVents: > statusCode: 0
2021-05-06 22:19:00.788 Status: dzVents: > isHardware: false
2021-05-06 22:19:00.788 Status: dzVents: > isDevice: false
2021-05-06 22:19:00.788 Status: dzVents: > isSystem: false
2021-05-06 22:19:00.788 Status: dzVents: > isGroup: false
2021-05-06 22:19:00.788 Status: dzVents: > isScene: false
2021-05-06 22:19:00.788 Status: dzVents: > isHTTPResponse: false
2021-05-06 22:19:00.788 Status: dzVents: > callback: SSH_0.20210506_01
2021-05-06 22:19:00.788 Status: dzVents: > timeoutOccurred: false
2021-05-06 22:19:00.788 Status: dzVents: > hasLines: false
2021-05-06 22:19:00.789 Status: dzVents: > isShellCommandResponse: true
2021-05-06 22:19:00.789 Status: dzVents: > ok: true
2021-05-06 22:19:00.789 Status: dzVents: > trigger: SSH_0.20210506_01
2021-05-06 22:19:00.789 Status: dzVents: Debug: SSH_0.20210506_01: Constructed timed-command: Off
2021-05-06 22:19:00.789 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 22:19:00.789 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 22:19:00.789 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = nil
2021-05-06 22:19:00.789 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-06 22:19:00.789 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-06 22:19:00.789 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW
2021-05-06 22:19:00.790 Status: EventSystem: Script event triggered: /opt/domoticz/dzVents/runtime/dzVents.lua
2021-05-06 22:19:00.824 Status: Notification: SSH_0.20210506_01
2021-05-06 22:19:00.789 Error: dzVents: Error: (3.1.8) SSH_0.20210506_01: Result: 2: OpenVPN not connected. Check it out
2021-05-06 22:19:01.254 Notification sent (email) => Failed

2021-05-06 22:19:01.254 Error: SMTP Mailer: Error sending Email to: <[email protected]> !
2021-05-06 22:19:01.254 Error: libcurl: (56)
2021-05-06 22:19:01.254 Error: GnuTLS recv error (-54): Error in the pull function.
2021-05-06 22:19:01.254
2021-05-06 22:19:01.254 Error: Failed to send Email notification! 
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by waaren »

Chris12 wrote: Thursday 06 May 2021 22:22 with the debug enabled:

Code: Select all

2021-05-06 22:19:00.788 Status: dzVents: > data: 2
2021-05-06 22:19:00.788 Status: dzVents: > statusCode: 0

Please change line

Code: Select all

 if ( statusCode ~= 0 ) or ( item.data ~= 2 ) then
to

Code: Select all

 if ( item.statusCode ~= 0 ) or ( item.data ~= 2 ) then
and try agian
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by Chris12 »

the log file now shows:

Code: Select all

 2021-05-06 22:34:00.191 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW:, trigger: "every minute"
2021-05-06 22:34:00.192 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 22:34:00.192 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 22:34:00.192 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 22:34:00.192 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = SSH_0.20210506_01
2021-05-06 22:34:00.192 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-06 22:34:00.192 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-06 22:34:00.192 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW
2021-05-06 22:34:00.507 Status: dzVents: Info: Handling shellcommandResponse-events for: "SSH_0.20210506_01"
2021-05-06 22:34:00.507 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW: ShellCommandResponse: "SSH_0.20210506_01"
2021-05-06 22:34:00.508 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 22:34:00.508 Status: dzVents: > errorText:
2021-05-06 22:34:00.508 Status: dzVents: > isSystem: false
2021-05-06 22:34:00.508 Status: dzVents: > isScene: false
2021-05-06 22:34:00.508 Status: dzVents: > isJSON: false
2021-05-06 22:34:00.508 Status: dzVents: > data: 2
2021-05-06 22:34:00.508 Status: dzVents: > isDevice: false
2021-05-06 22:34:00.508 Status: dzVents: > isTimer: false
2021-05-06 22:34:00.509 Status: dzVents: > baseType: shellcommandResponse
2021-05-06 22:34:00.509 Status: dzVents: > isCustomEvent: false
2021-05-06 22:34:00.509 Status: dzVents: > statusText:
2021-05-06 22:34:00.509 Status: dzVents: > ok: true
2021-05-06 22:34:00.509 Status: dzVents: > isHTTPResponse: false
2021-05-06 22:34:00.509 Status: dzVents: > timeoutOccurred: false
2021-05-06 22:34:00.509 Status: dzVents: > isGroup: false
2021-05-06 22:34:00.509 Status: dzVents: > callback: SSH_0.20210506_01
2021-05-06 22:34:00.509 Status: dzVents: > isShellCommandResponse: true
2021-05-06 22:34:00.509 Status: dzVents: > isXML: false
2021-05-06 22:34:00.509 Status: dzVents: > isVariable: false
2021-05-06 22:34:00.509 Status: dzVents: > isHardware: false
2021-05-06 22:34:00.509 Status: dzVents: > trigger: SSH_0.20210506_01
2021-05-06 22:34:00.510 Status: dzVents: > isSecurity: false
2021-05-06 22:34:00.510 Status: dzVents: > shellCommandResponse: SSH_0.20210506_01
2021-05-06 22:34:00.510 Status: dzVents: > statusCode: 0
2021-05-06 22:34:00.510 Status: dzVents: > hasLines: false
2021-05-06 22:34:00.510 Status: dzVents: > dump()
2021-05-06 22:34:00.510 Status: dzVents: Debug: SSH_0.20210506_01: Constructed timed-command: Off
2021-05-06 22:34:00.510 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 22:34:00.510 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 22:34:00.510 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = nil
2021-05-06 22:34:00.510 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-06 22:34:00.510 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-06 22:34:00.510 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW
2021-05-06 22:34:00.511 Status: EventSystem: Script event triggered: /opt/domoticz/dzVents/runtime/dzVents.lua
2021-05-06 22:34:00.542 Status: Notification: SSH_0.20210506_01
2021-05-06 22:34:00.510 Error: dzVents: Error: (3.1.8) SSH_0.20210506_01: Result: 2: OpenVPN not connected. Check it out
2021-05-06 22:34:01.159 Notification sent (email) => Failed
2021-05-06 22:34:01.159 Error: SMTP Mailer: Error sending Email to: <[email protected]> !
2021-05-06 22:34:01.159 Error: libcurl: (35)
2021-05-06 22:34:01.159 Error: gnutls_handshake() failed: Error in the pull function.
2021-05-06 22:34:01.159
2021-05-06 22:34:01.159 Error: Failed to send Email notification!
I just noticed that I get email, but only 1 time directly after the script restart. The notification email mentioning that the VPN is not connected.
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by waaren »

Chris12 wrote: Thursday 06 May 2021 22:39 the log file now shows:
OK it looks like the item.data (2) is in fact a string
so another change
change line

Code: Select all

if ( item.statusCode ~= 0 ) or ( item.data ~= 2 ) then
to

Code: Select all

if ( item.statusCode ~= 0 ) or ( tonumber(item.data) ~= 2 ) then
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by Chris12 »

Goodmorning, the log file shows now more information including the ping option to check the wan.

Code: Select all

2021-05-07 07:49:00.553 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW:, trigger: "every minute"
2021-05-07 07:49:00.566 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-07 07:49:00.566 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-07 07:49:00.566 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-07 07:49:00.566 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = SSH_0.20210506_01
2021-05-07 07:49:00.567 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-07 07:49:00.567 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-07 07:49:00.567 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW
2021-05-07 07:49:00.819 Status: dzVents: Info: Handling shellcommandResponse-events for: "SSH_0.20210506_01"
2021-05-07 07:49:00.820 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW: ShellCommandResponse: "SSH_0.20210506_01"
2021-05-07 07:49:00.832 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-07 07:49:00.832 Status: dzVents: > errorText:
2021-05-07 07:49:00.832 Status: dzVents: > isVariable: false
2021-05-07 07:49:00.833 Status: dzVents: > callback: SSH_0.20210506_01
2021-05-07 07:49:00.833 Status: dzVents: > isDevice: false
2021-05-07 07:49:00.833 Status: dzVents: > isSystem: false
2021-05-07 07:49:00.833 Status: dzVents: > isXML: false
2021-05-07 07:49:00.833 Status: dzVents: > isHTTPResponse: false
2021-05-07 07:49:00.833 Status: dzVents: > baseType: shellcommandResponse
2021-05-07 07:49:00.833 Status: dzVents: > data: 2
2021-05-07 07:49:00.833 Status: dzVents: > statusCode: 0
2021-05-07 07:49:00.833 Status: dzVents: > dump()
2021-05-07 07:49:00.833 Status: dzVents: > isCustomEvent: false
2021-05-07 07:49:00.833 Status: dzVents: > isScene: false
2021-05-07 07:49:00.833 Status: dzVents: > isHardware: false
2021-05-07 07:49:00.833 Status: dzVents: > isGroup: false
2021-05-07 07:49:00.833 Status: dzVents: > isSecurity: false
2021-05-07 07:49:00.833 Status: dzVents: > shellCommandResponse: SSH_0.20210506_01
2021-05-07 07:49:00.833 Status: dzVents: > isTimer: false
2021-05-07 07:49:00.833 Status: dzVents: > statusText:
2021-05-07 07:49:00.833 Status: dzVents: > ok: true
2021-05-07 07:49:00.833 Status: dzVents: > isShellCommandResponse: true
2021-05-07 07:49:00.833 Status: dzVents: > timeoutOccurred: false
2021-05-07 07:49:00.833 Status: dzVents: > trigger: SSH_0.20210506_01
2021-05-07 07:49:00.833 Status: dzVents: > isJSON: false
2021-05-07 07:49:00.833 Status: dzVents: > hasLines: false
2021-05-07 07:49:00.833 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'ping -c1 -w1 8.8.8.8 ;'
2021-05-07 07:49:01.041 Notification sent (browser) => Success
2021-05-07 07:49:01.043 VPN Status ON/OFF: Light/Switch (VPN Status ON/OFF)
2021-05-07 07:49:01.019 Status: dzVents: Debug: SSH_0.20210506_01: ReturnCode: 0
2021-05-07 07:49:01.019 commandOutput:PING 8.8.8.8 (8.8.8.8): 56 data bytes
2021-05-07 07:49:01.019 64 bytes from 8.8.8.8: seq=0 ttl=119 time=3.547 ms
2021-05-07 07:49:01.019
2021-05-07 07:49:01.019 --- 8.8.8.8 ping statistics ---
2021-05-07 07:49:01.019 1 packets transmitted, 1 packets received, 0% packet loss
2021-05-07 07:49:01.019 round-trip min/avg/max = 3.547/3.547/3.547 ms
2021-05-07 07:49:01.019
2021-05-07 07:49:01.020 Status: dzVents: Debug: SSH_0.20210506_01: OpenVPN (re)connected
2021-05-07 07:49:01.021 Status: dzVents: Debug: SSH_0.20210506_01: Constructed timed-command: On
2021-05-07 07:49:01.021 Status: dzVents: Debug: SSH_0.20210506_01: Constructed timed-command: On
2021-05-07 07:49:01.021 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW 
The returnCode is now shown, but I cannot see the commandOutput: 2, like seen in the old domoticz VPN script.

When running the new script the "VPN (re)connected" email is sent without any errors shown in the log, and that's correct as the VPN is still up&running.
I did receive 24 email this night of the script that the VPN was not connected. I think thats because the script parameter is set to 60 (minutes)?


when I disable the extended debug log, the domoticz logfile shows this:

Code: Select all

 2021-05-07 08:04:00.251 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW:, trigger: "every minute"
2021-05-07 08:04:00.252 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-07 08:04:00.252 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-07 08:04:00.252 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-07 08:04:00.252 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: callback = SSH_0.20210506_01
2021-05-07 08:04:00.252 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellCommand: timeout = 50
2021-05-07 08:04:00.252 Status: dzVents: Debug: SSH_0.20210506_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-07 08:04:00.252 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW

2021-05-07 08:04:00.442 Status: EventSystem: Script event triggered: /opt/domoticz/dzVents/runtime/dzVents.lua
2021-05-07 08:04:00.518 Status: dzVents: Info: Handling shellcommandResponse-events for: "SSH_0.20210506_01"
2021-05-07 08:04:00.518 Status: dzVents: Info: SSH_0.20210506_01: ------ Start internal script: VPN Status-NEW: ShellCommandResponse: "SSH_0.20210506_01"
2021-05-07 08:04:00.519 Status: dzVents: Debug: SSH_0.20210506_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-07 08:04:00.519 Status: dzVents: Debug: SSH_0.20210506_01: Executing Command: ssh [email protected] -p 2211 'ping -c1 -w1 8.8.8.8 ;'
2021-05-07 08:04:00.678 Status: dzVents: Debug: SSH_0.20210506_01: ReturnCode: 0
2021-05-07 08:04:00.678 commandOutput:PING 8.8.8.8 (8.8.8.8): 56 data bytes
2021-05-07 08:04:00.678 64 bytes from 8.8.8.8: seq=0 ttl=119 time=13.705 ms
2021-05-07 08:04:00.678
2021-05-07 08:04:00.678 --- 8.8.8.8 ping statistics ---
2021-05-07 08:04:00.678 1 packets transmitted, 1 packets received, 0% packet loss
2021-05-07 08:04:00.678 round-trip min/avg/max = 13.705/13.705/13.705 ms
2021-05-07 08:04:00.678
2021-05-07 08:04:00.679 Status: dzVents: Info: SSH_0.20210506_01: ------ Finished VPN Status-NEW 
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by waaren »

Chris12 wrote: Friday 07 May 2021 8:07 The returnCode is now shown, but I cannot see the commandOutput: 2, like seen in the old domoticz VPN script.
I did receive 24 email this night of the script that the VPN was not connected. I think thats because the script parameter is set to 60 (minutes)?
Should be fixed in below updated version

Code: Select all

local scriptVersion = '0.20210507_01'
local scriptVar  =  'SSH_' .. scriptVersion

--[[

This dzVents script is used to monitor open VPN state of a ASUS RT-AC86U router loaded with asuswrt-merlin firmware

The script use async executeShellCommand to trigger a nvram and when required do a service restart command on a remote system via ssh
(the router must be accessible by the user that is running the domoticz service, via password-less SSH (with public / private key setup)

Before activating the script:
    Read the GETTING STARTED section of the dzVents wiki.
    Change the values in the script to reflect your setup

requires:
    access to ssh and ping (extra actions needed to install these when in docker)
    passwordless access using ssh setup for the user executing domoticz
    domoticz >= V2020.2 12771 (dzVents >= 3.1.0)
    configured and working notification subsystem(s) and Email in domoticz


]]--

return
{
    on =
    {
        timer =
        {
            'every minute', -- change to required frequency
        },
        shellCommandResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = scriptVar,
    },

    execute = function(dz, item)

        local remoteHost = '192.168.1.1'   -- change to router IP
        local notConnectedMessage = 'OpenVPN not connected. Check it out'
        local reconnectedMessage = 'OpenVPN (re)connected'
        local wanDownMessage = 'Wan Connection down? Check it out'
        local remoteUser = 'Admin' -- domoticz uses user 'root' for executing commands, so the user 'root' should be password-less SSH access
        local remotePort = 2211
        local VPNStatus = dz.devices(1155)
        local notifyFrequency = 60 -- frequency in minutes

        -- =======================================================================
        --               NO changes required below this line
        -- =======================================================================

        --commands to execute remote
        local commands =
        {
            status = 'nvram get vpn_client1_state ',
            stop = 'service stop_vpnclient1 ',
            start = 'service start_vpnclient1 ',
            ping = 'ping -c1 -w1 8.8.8.8 ', -- ping command to check WAN connection available/down
        }
        commands.restart = commands.stop .. '; ' .. commands.start

        local function managedPopen(cmd)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
            dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_DEBUG)
            else -- all is fine!!
                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:' .. commandOutput, dz.LOG_DEBUG)
            end
            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

        local function osCommand(cmd, callback, timeout)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
            local timeout = timeout or 50

            dz.executeShellCommand(
            {
                command = cmd,
                callback = callback,
                timeout = timeout,
            })
        end

        local function buildSSHCommand(cmd)
            local sshBOL = "ssh " .. remoteUser .. "@" .. remoteHost .. " -p " .. remotePort .. " \'"
            local sshEOL = ";\'"
            local cmd = sshBOL .. cmd .. sshEOL
            return cmd
        end

        -- Main
        if item.isTimer then
            osCommand(buildSSHCommand(commands.status), scriptVar)
        else -- callback of the executeShellCommand

          --- dz.utils.dumpTable(item) -- Debug only
          dz.log('Resultcode: ' .. item.data, dz.LOG_DEBUG)
          if ( item.statusCode ~= 0 ) or ( tonumber(item.data) ~= 2 ) then
                if VPNStatus.state == 'On' or VPNStatus.lastUpdate.minutesAgo > notifyFrequency then
                    dz.log('Result: ' .. item.data .. ': ' .. notConnectedMessage, dz.LOG_ERROR)
                    dz.notify(scriptVar, notConnectedMessage, dz.PRIORITY_HIGH)
                    VPNStatus.switchOff()
                end
                osCommand(buildSSHCommand(commands.restart))
            else -- seems OK
                local result, rc = managedPopen(buildSSHCommand(commands.ping))
                if rc ~= 0 then
                    dz.log('VPN status is active but ping failed, internet WAN connection down?', dz.LOG_ERROR)
                    dz.notify(scriptVar, wanDownMessage, dz.PRIORITY_HIGH)
                elseif VPNStatus.state == 'Off' then
                    dz.log(reconnectedMessage, dz.LOG_DEBUG)
                    dz.notify(scriptVar, reconnectedMessage, dz.PRIORITY_LOW)
                    VPNStatus.switchOn().checkFirst()
                end
            end
        end
    end
}




Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Chris12
Posts: 238
Joined: Tuesday 18 August 2020 8:41
Target OS: NAS (Synology & others)
Domoticz version: 2021.1
Location: NL
Contact:

Re: Detection VPN connection ASUS Merlin Router

Post by Chris12 »

@waaren, thanks for creating and fixing the script, and offcourse with your patience to help me out!

Code: Select all

2021-05-07 18:55:00.511 Status: dzVents: Info: SSH_0.20210507_01: ------ Start internal script: VPN Status-NEW:, trigger: "every minute"
2021-05-07 18:55:00.524 Status: dzVents: Debug: SSH_0.20210507_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-07 18:55:00.524 Status: dzVents: Debug: SSH_0.20210507_01: Executing Command: ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-07 18:55:00.524 Status: dzVents: Debug: SSH_0.20210507_01: ExecuteShellCommand: command = ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-07 18:55:00.524 Status: dzVents: Debug: SSH_0.20210507_01: ExecuteShellCommand: callback = SSH_0.20210507_01
2021-05-07 18:55:00.524 Status: dzVents: Debug: SSH_0.20210507_01: ExecuteShellCommand: timeout = 50
2021-05-07 18:55:00.524 Status: dzVents: Debug: SSH_0.20210507_01: ExecuteShellcommand: path = /opt/domoticz/userdata/scripts/dzVents/data/
2021-05-07 18:55:00.524 Status: dzVents: Info: SSH_0.20210507_01: ------ Finished VPN Status-NEW
2021-05-07 18:55:01.684 Status: dzVents: Info: Handling shellcommandResponse-events for: "SSH_0.20210507_01"
2021-05-07 18:55:01.684 Status: dzVents: Info: SSH_0.20210507_01: ------ Start internal script: VPN Status-NEW: ShellCommandResponse: "SSH_0.20210507_01"
2021-05-07 18:55:01.685 Status: dzVents: Debug: SSH_0.20210507_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-07 18:55:01.685 Status: dzVents: Debug: SSH_0.20210507_01: Resultcode: 2
2021-05-07 18:55:01.685 Status: dzVents: Debug: SSH_0.20210507_01: Executing Command: ssh [email protected] -p 2211 'ping -c1 -w1 8.8.8.8 ;'
2021-05-07 18:55:01.862 Status: dzVents: Debug: SSH_0.20210507_01: ReturnCode: 0
2021-05-07 18:55:01.862 commandOutput:PING 8.8.8.8 (8.8.8.8): 56 data bytes
2021-05-07 18:55:01.862 64 bytes from 8.8.8.8: seq=0 ttl=119 time=3.527 ms
2021-05-07 18:55:01.862
2021-05-07 18:55:01.862 --- 8.8.8.8 ping statistics ---
2021-05-07 18:55:01.862 1 packets transmitted, 1 packets received, 0% packet loss
2021-05-07 18:55:01.862 round-trip min/avg/max = 3.527/3.527/3.527 ms
2021-05-07 18:55:01.862
2021-05-07 18:55:01.862 Status: dzVents: Info: SSH_0.20210507_01: ------ Finished VPN Status-NEW 
Domoticz beta | Dashticz beta | Synology DS415+ | Wall tablet Teclast 11.6inch (Android) | TADO v3 controlled heating
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest