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: Monday 03 May 2021 11:11 But it always nice to have an extra check/fallback done via the script.
Added an extra check using the ping command in below version. Only a log message is produced when the ping fails. If you want / need any other notifications please try to include them yourself.

Code: Select all

local scriptVersion = '0.20210503_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 io.popen 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

]]--

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

    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 remoteUser = 'Admin'
        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 8.8.8.8  ',
        }
        commands.restart = commands.stop .. '; ' .. commands.start

        local function osCommand(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:\n' .. commandOutput, dz.LOG_DEBUG)
            end
            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

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

            local cmd = sshBOL .. cmd .. sshEOL
            return cmd
        end

        -- Main
        local result, rc = osCommand(buildSSHCommand(commands.status))
        dz.log(result,dz.LOG_DEBUG)
        if tonumber(result) ~= 2 then
            if VPNStatus.state == 'On' or VPNStatus.lastUpdate.minutesAgo > notifyFrequency then
                dz.log('Result: ' .. result .. ': ' .. notConnectedMessage, dz.LOG_ERROR)
                dz.notify(scriptVar, notConnectedMessage, dz.PRIORITY_HIGH)
                VPNStatus.switchOff().checkFirst()
            end
            osCommand(buildSSHCommand(commands.restart))
        else
            local result, rc = osCommand(buildSSHCommand(commands.ping))
            if rc ~= 0 then 
                dz.log('VPN status is active but ping failed', dz.LOG_ERROR)
            elseif VPNStatus.state == 'Off' then
                dz.log(reconnectedMessage, dz.LOG_DEBUG)
                dz.notify(scriptVar, reconnectedMessage, dz.PRIORITY_LOW)
                VPNStatus.switchOn().checkFirst()
            end
        end
    end
}

One other question:
Where in the script are the 'empty' lines defined? And can the 'commandOutput:' have the result value directly behing it, like the 'ReturnCode' has?
[/quote]
The '\n' in the script causes a CR/LF in the log. If you don't want that you can remove it.
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 »

Thanks waaren!

I editted the script, and let it run. That goes fine, but it seems that the ping command does never stop?
And my domoticz is getting unresponsive.

Code: Select all

2021-05-03 13:14:10.011 Error: EventSystem: Warning!, lua script /usr/local/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
Maybe the number of pings should be defined somewhere?
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: Monday 03 May 2021 13:20 Maybe the number of pings should be defined somewhere?
Yes.


Change line

Code: Select all

ping = 'ping 8.8.8.8  ',
to

Code: Select all

ping = 'ping 8.8.8.8  -c1 -w1',      -- ping 1 time and wait max. 1 second
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 »

Thanks! I was allready playing arround with this option:

Code: Select all

ping = 'ping -c 4 8.8.8.8 ', -- ping command to check WAN connection available/down
Which gives 4 pings and no warning message of dzvents.

when using your code and have for example 2 pings, only 1 ping is done?
It looks like the w1 part is not working with that,when using w2 then it works!

Code: Select all

root@DS415:~# ping 8.8.8.8  -c2 -w1
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=8.74 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.744/8.744/8.744/0.000 ms

root@DS415:~# ping 8.8.8.8  -c2
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=7.50 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=7.47 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 7.478/7.491/7.505/0.087 ms
root@DS415:~#

root@DS415:~# ping 8.8.8.8  -c2 -w2
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=7.64 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=7.37 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 7.379/7.510/7.641/0.131 ms
root@DS415:~#

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: Monday 03 May 2021 13:49 It looks like the w1 part is not working with that,when using w2 then it works!
ping 8.8.8.8 -c1 -w1 should do 1 ping and max wait for 1 second.

Test with

res=$(ping 8.8.8.8 -c1 -w1 2>&1 >/dev/null ) && echo ping succesful || echo ping failed
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 »

Yeah, it all works fine! thanks again for the very quick help.

I also added a wanDownMessage notification, which I expect to be sent to the email (I copied it from the other messages part, and added the local function).
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 »

Hi @waaren,

I noticed that sometimes my domoticz is completely unresponsive, and the domoticz webpage is not working anymore and I have to restart my complete NAS to get domoticz working again.

I found the domoticz.log file an in there I see the following:
(I removed other MQTT and TADO thermostaat messages between)

Code: Select all

2021-05-05 10:07:00.264  Status: dzVents: Info: SSH_0.20210503_01: ------ Start internal script: VPN Status:, trigger: "every minute"
2021-05-05 10:07:00.276  Status: dzVents: Debug: SSH_0.20210503_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-05 10:07:00.276  Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'

2021-05-05 10:07:10.218  Error: EventSystem: Warning!, lua script /usr/local/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
2021-05-05 10:07:20.230  Error: EventSystem: Warning!, lua script /usr/local/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds

2021-05-05 10:08:15.791  Error: WebServer:8084 thread seems to have ended unexpectedly (last update 68.000000 seconds ago)

I'm not sure it has something to do with the script, but maybe there's a way that the script can be stopped/reset after like 5 seconds when there is no response from the "Executing Command: sudo ssh [email protected] -p 2211 'nvram get vpn_client1_state ;' "?
And then for example try it again after 30secs? Or let the regular programmed next try (within 1 minute) then run, but give not yet a restart of the VPN clent and an error email when ever the reset after 5 sec has kicked in.

when it goes OK I can see these timings:

Code: Select all

2021-05-05 07:26:00.450  Status: dzVents: Info: SSH_0.20210503_01: ------ Start internal script: VPN Status:, trigger: "every minute"
2021-05-05 07:26:00.451  Status: dzVents: Debug: SSH_0.20210503_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-05 07:26:00.451  Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-05 07:26:00.746  Status: dzVents: Debug: SSH_0.20210503_01: ReturnCode: 0
commandOutput:2

2021-05-05 07:26:00.746  Status: dzVents: Debug: SSH_0.20210503_01: 2

2021-05-05 07:26:00.746  Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'ping -c1 -w1 8.8.8.8 ;'
2021-05-05 07:26:01.062  Status: dzVents: Debug: SSH_0.20210503_01: ReturnCode: 0
commandOutput:PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=118 time=27.578 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 27.578/27.578/27.578 ms

2021-05-05 07:26:01.062  Status: dzVents: Info: SSH_0.20210503_01: ------ Finished VPN Status
So when it goes OK, the check and ping is done within 2 seconds.
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: Wednesday 05 May 2021 13:07 I noticed that sometimes my domoticz is completely unresponsive, and the domoticz webpage is not working anymore and I have to restart my complete NAS to get domoticz working again.
It seems that the SSH command hangs. There are a number of solutions for that advised all mentioning changing the ssh config-file.
Because io.popen is a blocking command in the EventSystem it could hang that thread. It should not cause any problems for the other threads.

I suspect something in the network, causing the issue for the SSH command also prevent access to domoticz / your NAS.

If you can update to the recent release (2021.1) or a recent beta you can make use of the new and async (= non blocking) dzVents function executeShellCommand. This will prevent the 10 seconds type of messages.
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 »

Hi, as Im running domoticz as a package on my Synology, there is no simple option to upgrade to the 2021 version. No package available afaik, the person creating those has stopped (jadahl).

I do not have any docker experience which is often mentioned as a sollution to run the New domoticz version. Maybe I need to look for a simple step-by-step guide domoticz+synology+docker.

Only domoticz is hanging, not my NAS. The network is fine, no issues there. But in order to get the domoticz package to work again, normally a stop/start of a package should be fine. But after stopping the domoticz package, it wont start again until I completelly reboot my NAS.
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 »

So I managed to get a docker running with the latest domoticz:

Code: Select all

2021-05-05 21:17:37.522 Status: Domoticz V2021.1 (build 13243) (c)2012-2021 GizMoCuz
2021-05-05 21:17:37.522 Status: Build Hash: c11443402, Date: 2021-05-02 09:21:16
2021-05-05 21:17:37.522 Status: Startup Path: /opt/domoticz/
2021-05-05 21:17:37.629 Status: PluginSystem: Started, Python version '3.7.3'.
2021-05-05 21:17:37.634 Status: WebServer(HTTP) started on address: :: with port 8085 
buy somehow I have an issue with permissions on the scripts (imported from backup)

Code: Select all

2021-05-05 20:56:07.398 Error: EventSystem: problem writing file: /opt/domoticz/userdata/scripts/dzVents/generated_scripts/Monit-status-data.lua
2021-05-05 20:56:07.398 Error: EventSystem: problem writing file: /opt/domoticz/userdata/scripts/dzVents/generated_scripts/VPN Status.lua
2021-05-05 20:56:07.398 Error: EventSystem: problem writing file: /opt/domoticz/userdata/scripts/dzVents/generated_scripts/Luftdaten.lua 
Not found a sollution yet, as the directory I cannot find using root?
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: Wednesday 05 May 2021 21:50 So I managed to get a docker running with the latest domoticz:
Not found a sollution yet, as the directory I cannot find using root?
For now just create this (sub)directory within docker.
Have a look at this-, and subsequent posts in the same topic.
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 »

thanks! The script write error is solved!

in the domoticz (docker installation) log I now see this: dzVents: Debug: SSH_0.20210503_01: Error ==>> sh: 1: sudo: not found

Code: Select all

 2021-05-06 07:30:00.266 Status: dzVents: Info: SSH_0.20210503_01: ------ Start internal script: VPN Status:, trigger: "every minute"
2021-05-06 07:30:00.267 Status: dzVents: Debug: SSH_0.20210503_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 07:30:00.267 Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 07:30:00.272 Status: dzVents: Debug: SSH_0.20210503_01: Error ==>> sh: 1: sudo: not found
2021-05-06 07:30:00.272 Status: dzVents: Debug: SSH_0.20210503_01: sh: 1: sudo: not found
2021-05-06 07:30:00.272 ::ERROR::
2021-05-06 07:30:00.272
2021-05-06 07:30:00.273 Status: dzVents: Debug: SSH_0.20210503_01: Constructed timed-command: Off
2021-05-06 07:30:00.273 Status: dzVents: Debug: SSH_0.20210503_01: Constructed timed-command: Off
2021-05-06 07:30:00.273 Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 07:30:00.279 Status: dzVents: Debug: SSH_0.20210503_01: Error ==>> sh: 1: sudo: not found
2021-05-06 07:30:00.279 Status: dzVents: Info: SSH_0.20210503_01: ------ Finished VPN Status
2021-05-06 07:30:00.280 Status: EventSystem: Script event triggered: /opt/domoticz/dzVents/runtime/dzVents.lua
2021-05-06 07:30:00.301 Status: Notification: SSH_0.20210503_01
2021-05-06 07:30:00.273 Error: dzVents: Error: (3.1.8) SSH_0.20210503_01: Result: sh: 1: sudo: not found
In the 'normal' domoticz there is no issue with the VPN, still up&running, and the command is working fine.
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 7:36 in the domoticz (docker installation) log I now see this: dzVents: Debug: SSH_0.20210503_01: Error ==>> sh: 1: sudo: not found
sudo is a program and need to be installed and accessible. If the user executing domoticz in docker is already root the sudo parts are not needed.
Check this explanation

If you still have questions around this or other docker related stuff then best to ask them in one of the topics on this forum where default docker installation is discussed.
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 pointing me to the correct info. You're right about the docker knowlegde and topics, but sometimes I'm not sure weather it's docker related or script releated as in both I'm new (but willing to learn!)

I had indeed not installed the sudo package. Now that error is solved I think, but the script still gives me troubles when running:
Error ==>> sudo: unable to resolve host domoticz-domoticz: No address associated with hostname

Code: Select all

021-05-06 10:52:00.529 Status: dzVents: Info: SSH_0.20210503_01: ------ Start internal script: VPN Status:, trigger: "every minute"
2021-05-06 10:52:00.529 Status: dzVents: Debug: SSH_0.20210503_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 10:52:00.529 Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'nvram get vpn_client1_state ;'
2021-05-06 10:52:00.580 Status: dzVents: Debug: SSH_0.20210503_01: Error ==>> sudo: unable to resolve host domoticz-domoticz: No address associated with hostname
2021-05-06 10:52:00.580 sudo: ssh: command not found
2021-05-06 10:52:00.580 Status: dzVents: Debug: SSH_0.20210503_01: sudo: unable to resolve host domoticz-domoticz: No address associated with hostname
2021-05-06 10:52:00.580 sudo: ssh: command not found
2021-05-06 10:52:00.580 ::ERROR::
2021-05-06 10:52:00.580
2021-05-06 10:52:00.580 Status: dzVents: Debug: SSH_0.20210503_01: Constructed timed-command: Off
2021-05-06 10:52:00.580 Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'service stop_vpnclient1 ; service start_vpnclient1 ;'
2021-05-06 10:52:00.630 Status: dzVents: Debug: SSH_0.20210503_01: Error ==>> sudo: unable to resolve host domoticz-domoticz: No address associated with hostname
2021-05-06 10:52:00.630 sudo: ssh: command not found
2021-05-06 10:52:00.630 Status: dzVents: Info: SSH_0.20210503_01: ------ Finished VPN Status
2021-05-06 10:52:00.639 Status: Notification: SSH_0.20210503_01 
So checked the /etc/hosts and /etc/hostname, and changed it to the same I have on my synology (which runs the domoticz package, and have no errors in the script).

Code: Select all

hostname DS415

cat /etc/hosts
127.0.0.1 localhost
::1 localhost
127.0.1.1  DS415

cat /etc/hostname
DS415

But still seeing the same error message.
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 11:48 But still seeing the same error message.
ssh is also an installable program and according to the error message not installed and/or not accessible from within 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 »

Hi @waaren, thank you for your patience to help me out. I just figured the ssh part out myself as well. So I now have apt-get install openssh-server done in my docker as well, and I an use the ssh command inside the domoticz docker. (I was asked a password again, should I re-do the passwordless key stuff?)

Also the hostname issue was away, but after a restart of the docker container the hostname is changed back to domoticz-domoticz ??

But I'm now back with the initial issue I started to use a docker domotcz container

Code: Select all

2021-05-06 12:40:00.989 Status: dzVents: Info: SSH_0.20210503_01: ------ Start internal script: VPN Status:, trigger: "every minute"
2021-05-06 12:40:00.990 Status: dzVents: Debug: SSH_0.20210503_01: Processing device-adapter for VPN Status ON/OFF: Switch device adapter
2021-05-06 12:40:00.990 Status: dzVents: Debug: SSH_0.20210503_01: Executing Command: sudo ssh [email protected] -p 2211 'nvram get vpn_client1_state ;' 

2021-05-06 12:40:10.920 Error: EventSystem: Warning!, lua script /opt/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds

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 12:51 I was asked a password again, should I re-do the password-less key stuff?
Probably yes, because ssh will now look inside the docker environment for the .ssh/authorized_keys and .ssh/known_hosts files.
But I'm now back with the initial issue I started to use a docker domoticz container
Because you now have a more recent version of domoticz inside your docker, you can make use of dzVents new async executeShellCommand().
What is your domoticz version inside docker?

Once you managed to get the password-less ssh working in docker, I will update the script to use the executeShellComamnd feature.
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, passwordless connection from docker to the ASUS router is working, I had to generate a new key-pair as now it is the root@domoticz-domoticz accessing the router.

the version of domoticz: Version: 2021.1 (build 13243)
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 14:11 @waaren, passwordless connection from docker to the ASUS router is working, I had to generate a new key-pair as now it is the root@domoticz-domoticz accessing the router.

the version of domoticz: Version: 2021.1 (build 13243)
OK. Please share the script as you have it now (here or via DM)
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 script:

Code: Select all

local scriptVersion = '0.20210503_02'
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 io.popen 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
    
]]--

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

    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 osCommand(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 buildSSHCommand(cmd)
            local sshBOL = "ssh " .. remoteUser .. "@" .. remoteHost .. " -p " .. remotePort .. " \'"
            local sshEOL = ";\'"

            local cmd = sshBOL .. cmd .. sshEOL
            return cmd
        end

        -- Main
        local result, rc = osCommand(buildSSHCommand(commands.status))
        dz.log(result,dz.LOG_DEBUG)
        if tonumber(result) ~= 2 then
            if VPNStatus.state == 'On' or VPNStatus.lastUpdate.minutesAgo > notifyFrequency then
                dz.log('Result: ' .. result .. ': ' .. notConnectedMessage, dz.LOG_ERROR)
                dz.notify(scriptVar, notConnectedMessage, dz.PRIORITY_HIGH)
                VPNStatus.switchOff().checkFirst()
            end
            osCommand(buildSSHCommand(commands.restart))
        else
            local result, rc = osCommand(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
}



One thing noticed as well, but only seen once:

Code: Select all

2021-05-06 14:18:01.286 Error: SMTP Mailer: Error sending Email to: <[email protected]> !
2021-05-06 14:18:01.286 Error: libcurl: (67)
2021-05-06 14:18:01.286 Error: Login denied
2021-05-06 14:18:01.286
2021-05-06 14:18:01.286 Error: Failed to send Email notification! 
This looks like a missing library as well isn't it?
I already have curl installed.
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