Code Snippet

Just another Code Snippet site

[Domoticz] Lua : ping host

commandArray = {}
 
 ping_success=os.execute('ping -c1 192.168.1.156')
 if ping_success then
   print("ping success")
   commandArray['Ping']='On'
 else
   print("ping fail")
   commandArray['Ping']='Off'	
 end
 
return commandArray

--Initialise la commande de retour finale
commandArray={}
 
 
--Mode deboggage (affichage des messages)
debug=true
--Prefixe pour les sorties de log
prefixe="(PING) "
 
 
--Tableau des périphériques à "pinguer"
-- Key = adresse ip à pinguer
-- Value = périphérique virtuel à switcher
local ping={}
ping['192.168.0.200']='Presence Tom'
ping['192.168.0.201']='Presence Steph'
ping['192.168.0.20']='Presence Camera Interieure'
ping['192.168.0.21']='Presence Camera Exterieure'
ping['192.168.0.100']='Presence Raspberry'
 
--pour chaque entree du tableau
for ip, switch in pairs(ping) do
 
 --Le Ping ! : -c1 = Un seul ping  ,   -w1 délai d'une seconde d'attente de réponse
 ping_success=os.execute('ping -c1 -w1 '..ip)
 
 
 --Si le ping à répondu 
 if ping_success then
   if(debug==true)then
    print(prefixe.."ping success "..switch)
   end
   --si le switch etait sur off on l'allume
   if(otherdevices[switch]=='Off') then
      commandArray[switch]='On'
   end
 else
 
 --Si pas de réponse
   if(debug==true)then
    print(prefixe.."ping fail "..switch)
   end
   --si le switch etait sur oN on l'eteind
   if(otherdevices[switch]=='On') then
      commandArray[switch]='Off'
   end
 end 
end
 
return commandArray


Comments are currently closed.