Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import logging
  5. import RPi.GPIO as GPIO
  6. import os
  7. import time
  8. from simplemail import Email
  9.  
  10. GPIO.setmode(GPIO.BOARD)
  11. GPIO.setup(7, GPIO.OUT)
  12. GPIO.setup(11, GPIO.OUT)
  13. socket_right = 7
  14. socket_left = 11
  15. off = GPIO.LOW
  16. on = GPIO.HIGH
  17. online = 0
  18. offline = 1
  19. logging.basicConfig(filename='modem.log', format='%(levelname)s %(asctime)s %(message)s', level=logging.INFO)
  20.  
  21. def SendMail(msg):
  22. mail = Email(
  23. smtp_server = "smtp.gmail.com:465",
  24. smtp_user = "",
  25. smtp_password = "",
  26. use_ssl = True,
  27. from_address = "",
  28. to_address = "",
  29. subject = "Modem TC7200",
  30. message = msg
  31. )
  32. mail.send()
  33.  
  34. def SocketLeftRestart():
  35. logging.debug('SocketLeftRestart wurde aufgerufen')
  36. GPIO.output(socket_left, off)
  37. time.sleep(10)
  38. GPIO.output(socket_left, on)
  39. time.sleep(180)
  40.  
  41. def SocketRightRestart():
  42. logging.debug('SocketRightRestart wurde aufgerufen')
  43. GPIO.output(socket_right, off)
  44. time.sleep(10)
  45. GPIO.output(socket_right, on)
  46. time.sleep(180)
  47.  
  48. def RestartSockets():
  49. logging.debug('RestartSockets wurde aufgerufen')
  50. GPIO.output(socket_right, off)
  51. GPIO.output(socket_left, off)
  52. time.sleep(10)
  53. GPIO.output(socket_left, on)
  54. GPIO.output(socket_right, on)
  55. time.sleep(180)
  56.  
  57. def doPing(destination, ipx, lastStatus, msg):
  58. ping = "ping"
  59. if ipx == 6:
  60. ping+="6"
  61. ping+=" -q -c 1 " + destination
  62. logging.debug('gebautes ping kommando = ' + ping)
  63. response = os.system(ping)
  64. if response == online:
  65. if lastStatus == offline:
  66. logging.info(destination + ' wieder erreichbar.')
  67. if msg != "":
  68. SendMail(msg)
  69. r2 = ""
  70. r1 = online
  71. else:
  72. if lastStatus == online:
  73. logging.info(destination + ' hat einmalig nicht geantwortet.')
  74. r2 = ""
  75. else:
  76. logging.info(destination + ' zweites mal nicht geantwortet, wird neugestartet.')
  77. r2 = time.strftime("%d.%m.%Y %H:%M") + " " + destination + " hat zwei mal nicht geantwortet, modem wurde neugestartet"
  78. r1 = offline
  79. return {'status':r1, 'msg':r2 }
  80.  
  81.  
  82. logging.info('Script check modem gestartet')
  83. gateway = "192.168.0.1"
  84. wan = "ping.eu"
  85. status = online
  86. tick = 0
  87. tock = 120 #jede 10 minuten soll ein ping ins internet gehen
  88. msg = "" #wenn eine internetverbindung wieder besteht wird die nachricht per mail versendet
  89. while 1:
  90. time.sleep(5)
  91. response = os.system("ping -q -c 1 " + gateway)
  92. tick+=1
  93. if response == online:
  94. if status == offline:
  95. logging.info(gateway + ' wieder erreichbar.')
  96. if msg != "":
  97. SendMail(msg)
  98. msg = ""
  99. status = online
  100. if tick == tock:
  101. tick = 0
  102. if os.system("ping -q -c 1 " + wan) != online:
  103. status = offline
  104. logging.info(wan + ' ist nicht erreichbar.')
  105. msg = time.strftime("%d.%m.%Y %H:%M") + " " + wan + " war nicht erreichbar, modem wurde neugestartet."
  106. RestartSockets()
  107. else:
  108. if status == online:
  109. logging.info(gateway + ' hat einmalig nicht geantwortet.')
  110. msg = ""
  111. else:
  112. logging.info(gateway + ' zweites mal nicht geantwortet, wird neugestartet.')
  113. msg = time.strftime("%d.%m.%Y %H:%M") + " " + gateway + " hat zwei mal nicht geantwortet, wurde neugestartet"
  114. tick = 0
  115. RestartSockets()
  116. status = offline
  117.  
  118. logging.info('Script check modem beendet??')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement