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.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)
- 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 SocketLeftRestart():
- logging.debug('SocketLeftRestart wurde aufgerufen')
- GPIO.output(socket_left, off)
- time.sleep(10)
- GPIO.output(socket_left, on)
- time.sleep(180)
- def SocketRightRestart():
- logging.debug('SocketRightRestart wurde aufgerufen')
- GPIO.output(socket_right, off)
- time.sleep(10)
- GPIO.output(socket_right, on)
- time.sleep(180)
- def RestartSockets():
- logging.debug('RestartSockets wurde aufgerufen')
- GPIO.output(socket_right, off)
- GPIO.output(socket_left, off)
- time.sleep(10)
- GPIO.output(socket_left, on)
- GPIO.output(socket_right, on)
- time.sleep(180)
- def doPing(destination, ipx, lastStatus, msg):
- ping = "ping"
- if ipx == 6:
- ping+="6"
- ping+=" -q -c 1 " + destination
- logging.debug('gebautes ping kommando = ' + ping)
- response = os.system(ping)
- if response == online:
- if lastStatus == offline:
- logging.info(destination + ' wieder erreichbar.')
- if msg != "":
- SendMail(msg)
- r2 = ""
- r1 = online
- else:
- if lastStatus == online:
- logging.info(destination + ' hat einmalig nicht geantwortet.')
- r2 = ""
- 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"
- wan = "ping.eu"
- status = online
- tick = 0
- tock = 120 #jede 10 minuten soll ein ping ins internet gehen
- msg = "" #wenn eine internetverbindung wieder besteht wird die nachricht per mail versendet
- while 1:
- time.sleep(5)
- response = os.system("ping -q -c 1 " + gateway)
- tick+=1
- if response == online:
- if status == offline:
- logging.info(gateway + ' wieder erreichbar.')
- if msg != "":
- SendMail(msg)
- msg = ""
- status = online
- if tick == tock:
- tick = 0
- if os.system("ping -q -c 1 " + wan) != online:
- status = offline
- logging.info(wan + ' ist nicht erreichbar.')
- msg = time.strftime("%d.%m.%Y %H:%M") + " " + wan + " war nicht erreichbar, modem wurde neugestartet."
- RestartSockets()
- else:
- if status == online:
- logging.info(gateway + ' hat einmalig nicht geantwortet.')
- msg = ""
- else:
- logging.info(gateway + ' zweites mal nicht geantwortet, wird neugestartet.')
- msg = time.strftime("%d.%m.%Y %H:%M") + " " + gateway + " hat zwei mal nicht geantwortet, wurde neugestartet"
- tick = 0
- RestartSockets()
- status = offline
- logging.info('Script check modem beendet??')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement