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