Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import logging
- import RPi.GPIO as GPIO
- import os
- import time
- from simplemail import Email
- GPIO.setwarnings(False)
- GPIO.cleanup()
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(7, GPIO.OUT)
- GPIO.setup(11, GPIO.OUT)
- socket_right = 7
- socket_left = 11
- off = GPIO.LOW
- on = GPIO.HIGH
- online = 0
- offline = 1
- logging.basicConfig(filename='modem.log', format='%(levelname)s %(asctime)s %(message)s', level=logging.INFO)#DEBUG#INFO
- def SendMail(msg):
- mail = Email(
- smtp_server = 'smtp.gmail.com:465',
- smtp_user = '',
- smtp_password = '',
- use_ssl = True,
- from_address = '',
- to_address = '',
- subject = 'Modem TC7200',
- message = msg)
- mail.send()
- def RestartSockets(left, right):
- logging.debug('RestartSockets wurde aufgerufen mit links = ' + str(left) + ' und rechts = '+ str(right))
- if left:
- GPIO.output(socket_left, off)
- if right:
- GPIO.output(socket_right, off)
- time.sleep(10)
- if left:
- GPIO.output(socket_left, on)
- if right:
- GPIO.output(socket_right, on)
- time.sleep(180)
- def doPing(destination, ipx, lastStatus, msg):
- logging.debug('doPing wurde aufgerufen mit destination ' + destination + ' ipx '+ str(ipx) + ' lastStatus ' + str(lastStatus) + ' msg ' + str(msg))
- r2 = 0
- ping = "ping"
- if ipx == 6:
- ping+='6'
- ping+=" -q -c 1 " + destination
- logging.debug('gebautes ping kommando = ' + ping)
- response = os.system(ping + ' > /dev/null 2>&1')
- if response == online:
- if lastStatus == offline:
- logging.info(destination + ' wieder erreichbar.')
- # if not msg:
- # SendMail(msg)
- r1 = online
- else:
- if lastStatus == online:
- logging.info(destination + ' hat einmalig nicht geantwortet.')
- else:
- logging.info(destination + ' zweites mal nicht geantwortet, wird neugestartet.')
- r2 = time.strftime('%d.%m.%Y %H:%M') + ' ' + destination + ' hat zwei mal nicht geantwortet, modem wurde neugestartet'
- r1 = offline
- return {'status':r1, 'msg':r2 }
- logging.info('Script check modem gestartet')
- gateway = '192.168.0.1'
- wan4 = 'unitymedia.de'
- wan6 = ''
- lanStat = online
- wan4Stat = online
- wan6Stat = online
- tick = 0
- tock = 60 #jede 5 minuten soll ein ping ins internet gehen
- msg = 0 #wenn eine internetverbindung wieder besteht wird die nachricht per mail versendet
- try:
- while 1:
- time.sleep(5)
- tick+=1
- result = doPing(gateway, 4, lanStat, msg)
- lanStat = result['status']
- msg = result['msg']
- if lanStat == offline and msg:
- RestartSockets(1 ,1)
- tick = 0
- continue
- if tick == tock:
- result = doPing(wan4, 4, wan4Stat, msg)
- wan4Stat = result['status']
- msg = result['msg']
- if wan4Stat == offline and msg:
- RestartSockets(1, 1)
- #result = doPing(wan6, 6, wan6Stat, msg)
- tick = 0
- finally:
- GPIO.cleanup()
- logging.info('script abgestürzt??')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement