Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 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.setwarnings(False)
  11. GPIO.cleanup()
  12. GPIO.setmode(GPIO.BOARD)
  13. GPIO.setup(7, GPIO.OUT)
  14. GPIO.setup(11, GPIO.OUT)
  15. socket_right = 7
  16. socket_left = 11
  17. off = GPIO.LOW
  18. on = GPIO.HIGH
  19. online = 0
  20. offline = 1
  21. logging.basicConfig(filename='modem.log', format='%(levelname)s %(asctime)s %(message)s', level=logging.INFO)#DEBUG#INFO
  22.  
  23. def SendMail(msg):
  24. mail = Email(
  25. smtp_server = 'smtp.gmail.com:465',
  26. smtp_user = '',
  27. smtp_password = '',
  28. use_ssl = True,
  29. from_address = '',
  30. to_address = '',
  31. subject = 'Modem TC7200',
  32. message = msg)
  33. mail.send()
  34.  
  35. def RestartSockets(left, right):
  36. logging.debug('RestartSockets wurde aufgerufen mit links = ' + str(left) + ' und rechts = '+ str(right))
  37. if left:
  38. GPIO.output(socket_left, off)
  39. if right:
  40. GPIO.output(socket_right, off)
  41. time.sleep(10)
  42. if left:
  43. GPIO.output(socket_left, on)
  44. if right:
  45. GPIO.output(socket_right, on)
  46. time.sleep(180)
  47.  
  48. def doPing(destination, ipx, lastStatus, msg):
  49. logging.debug('doPing wurde aufgerufen mit destination ' + destination + ' ipx '+ str(ipx) + ' lastStatus ' + str(lastStatus) + ' msg ' + str(msg))
  50. r2 = 0
  51. ping = "ping"
  52. if ipx == 6:
  53. ping+='6'
  54. ping+=" -q -c 1 " + destination
  55. logging.debug('gebautes ping kommando = ' + ping)
  56. response = os.system(ping + ' > /dev/null 2>&1')
  57. if response == online:
  58. if lastStatus == offline:
  59. logging.info(destination + ' wieder erreichbar.')
  60. # if not msg:
  61. # SendMail(msg)
  62. r1 = online
  63. else:
  64. if lastStatus == online:
  65. logging.info(destination + ' hat einmalig nicht geantwortet.')
  66. else:
  67. logging.info(destination + ' zweites mal nicht geantwortet, wird neugestartet.')
  68. r2 = time.strftime('%d.%m.%Y %H:%M') + ' ' + destination + ' hat zwei mal nicht geantwortet, modem wurde neugestartet'
  69. r1 = offline
  70. return {'status':r1, 'msg':r2 }
  71.  
  72. logging.info('Script check modem gestartet')
  73. gateway = '192.168.0.1'
  74. wan4 = 'unitymedia.de'
  75. wan6 = ''
  76. lanStat = online
  77. wan4Stat = online
  78. wan6Stat = online
  79. tick = 0
  80. tock = 60 #jede 5 minuten soll ein ping ins internet gehen
  81. msg = 0 #wenn eine internetverbindung wieder besteht wird die nachricht per mail versendet
  82. try:
  83. while 1:
  84. time.sleep(5)
  85. tick+=1
  86. result = doPing(gateway, 4, lanStat, msg)
  87. lanStat = result['status']
  88. msg = result['msg']
  89. if lanStat == offline and msg:
  90. RestartSockets(1 ,1)
  91. tick = 0
  92. continue
  93. if tick == tock:
  94. result = doPing(wan4, 4, wan4Stat, msg)
  95. wan4Stat = result['status']
  96. msg = result['msg']
  97. if wan4Stat == offline and msg:
  98. RestartSockets(1, 1)
  99.  
  100. #result = doPing(wan6, 6, wan6Stat, msg)
  101. tick = 0
  102. finally:
  103. GPIO.cleanup()
  104. logging.info('script abgestürzt??')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement