Example: Send new IP by E-mail once it is changed

Task

Make a program which will automatically send the new IP address from the Logic Machine once it’s changed

Add resident script with 60sec interval

Source code    
  1. require('json')
  2. data = io.readproc('if-json')
  3. data = json.decode(data)
  4. ip = data.eth0.inetaddr
  5.  
  6.  
  7. ip_old = storage.get('ip_old')
  8. if ip_old == nil then
  9. storage.set('ip_old', ip)
  10. end
  11.  
  12. if ip ~= ip_old then
  13. subject = 'LM IP changed'
  14. message = 'The new IP is: ' .. ip
  15. mail('user@example.com', subject, message)
  16. storage.set('ip_old', ip)
  17. end