Skip to content

Instantly share code, notes, and snippets.

@B17C0D3
Last active November 20, 2016 23:07
Show Gist options
  • Save B17C0D3/f3e4103aca3ff6161ab538923adefaa2 to your computer and use it in GitHub Desktop.
Save B17C0D3/f3e4103aca3ff6161ab538923adefaa2 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# pip install python-simplemail
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
GATEWAY = {'name': '192.168.0.1', 'try': 0, 'max': 2, 'stat': ONLINE, 'ipv6': 0}
WAN4 = {'name' :'unitymedia.de', 'try': 0, 'max': 5, 'stat': ONLINE, 'ipv6': 0}
WAN6 = {'name' :'computerbase.de', 'try': 0, 'max': 5, 'stat': ONLINE, 'ipv6': 1}
WAIT_LOOP = 5 # 5 secunden zwichen jedem ping
WAIT_RESTART = 180 # 180 zeit für das modem zu starten und verbinden
TICK = 0 # one tick is 5 seconds
TOCK = 12 # jede minute
msg = 0 # die nachricht per mail versendet
logging.basicConfig(filename='/media/modem/modem.log',
format='%(levelname)s %(asctime)s %(message)s',
level=logging.INFO # DEBUG oder 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(5)
if left:
GPIO.output(SOCKET_LEFT, ON)
if right:
GPIO.output(SOCKET_RIGHT, ON)
def ping(p):
logging.debug('ping wurde aufgerufen Ziel:' + p['name']
+ ' Status:' + str(p['stat'])
+ ' Try:' + str(p['try'])
+ ' Max:' + str(p['max']))
result = ''
ping_command = 'ping'
if p['ipv6'] == 1:
ping_command += '6'
ping_command += ' -q -c 1 ' + p['name'] + ' > /dev/null 2>&1'
logging.debug(ping_command)
response = os.system(ping_command)
if response == ONLINE:
if p['stat'] == OFFLINE:
logging.info(p['name'] + ' wieder erreichbar.')
p['try'] = 0
p['stat'] = ONLINE
else:
p['try'] += 1
logging.info(p['name'] + ' antwortet zum '
+ str(p['try']) + 'ten mal nicht.')
if p['try'] == p['max']:
p['stat'] = OFFLINE
logging.info('Limit von ' + str(p['max']) + ' wurde erreicht.')
result = "{0} {1} antwortet zum {2}ten mal nicht. Modem wird neugestartet".format(time.strftime('%d.%m.%Y %H:%M'), p['name'], p['try'])
return result
logging.info('Script check modem gestartet')
try:
while 1:
time.sleep(WAIT_LOOP)
TICK += 1
result = ping(GATEWAY)
if GATEWAY['stat'] == OFFLINE and result:
RestartSockets(1, 1)
GATEWAY['try'] = 0
time.sleep(WAIT_RESTART)
TICK = 0
continue
if TICK == TOCK:
TICK = 0
result = ping(WAN4)
if WAN4['stat'] == OFFLINE and result:
RestartSockets(1, 1)
WAN4['try'] = 0
time.sleep(WAIT_RESTART)
continue
result = ping(WAN6)
if WAN6['stat'] == OFFLINE and result:
RestartSockets(1, 1)
WAN6['try'] = 0
time.sleep(WAIT_RESTART)
else:
if not(GATEWAY['try']) and not(WAN4['try']) and not(WAN6['try']) and msg:
SendMail(msg)
msg = 0
finally:
GPIO.cleanup()
logging.info('script abgestürzt??')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment