Hi Leute,
ich hab da ein kleines Problem bei dem Pythonscript welches meinen NAS-Server starten und stoppen soll, wenn wir zu Hause sind oder weg gehen.
Funktionieren tut das ganze über die Pings unserer Handys. Das Ganze klappt auch schon recht gut.
Das Problem habe ich morgens. Ab 05:45:00 Uhr soll das Script den Server starten, wenn die Handys im W-LAN verfügbar sind. Es passiert aber nichts.
Wenn ich die betreffenden Zeilen direkt in der Python Konsole eintrage bekomme ich true geliefert.
(now > "05:45:00" and now < "22:00:00" and wDay > "0" and wDay < "6") or (now > "08:30:00" and now < "22:00:00" and (wDay == "0" or wDay == "6"))
os.system("ping -c 1 " + ip1) == 0 or os.system("ping -c 1 " + ip2) == 0
Wenn wir Nachmittags von der Arbeit kommen startet das Script die NAS anstandslos. Auch das Abschalten beim verlassen des W-LANs funktioniert prima.
Gestartet wurde das Script über "python startStopNAS.py&" in der rc.local
Heute morgen habe ich es mal gekillt und manuell gestartet, da hat es sofort die NAS gestartet.
Ich bin doch etwas verwirrt.
Hat vlt. jemand nen Tipp, wo ich einen logischen Fehler habe? Oder was an der Programmierung nicht stimmt bezüglich des Zeitabgleiches? Was mich nur wundert, ist dass es nach manuellem Start lief.
Bin neu in Python und hab da noch ein paar Feinheiten aufzuholen.
Anbei mal der ganze Code:
import os, time
# Definitionen
ip1 = "192.168.X.X"
ip2 = "192.168.X.Y"
nasIP = "192.168.X.Z"
nasMAC = "mac"
winUser = "UserName"
winUserPW = "*****"
offCountIP1 = 0
offCountIP2 = 0
###
# Funktionen
def writeToLog(myLogText):
myFile = open("/home/mypi/startStopNASLog.txt", "a")
myFile.write(time.strftime("%d.%m.%Y - %H:%M:%S") + ": " + myLogText)
myFile.close()
###
# Ablauf
while True:
# NAS onlinestatus auslesen
if os.system("ping -c 1 " + nasIP) == 0:
onlineStatus = 1
else:
onlineStatus = 0
if onlineStatus == 1:
offCountIP1 = 0
offCountIP2 = 0
# NAS ist online. Handys Pingen
while True:
time.sleep(10)
if offCountIP1 >= 5 and offCountIP2 >= 5:
# Log schreiben
writeToLog("Stoppe NAS. => offCount IP1: " + str(offCountIP1) + " / offCount IP2: " + str(offCountIP2) + "\n")
os.system("sudo net rpc shutdown -f -I " + nasIP + " -U " + winUser + "%" + winUserPW)
# Sleep, damit der shutdown-Befehl nicht 4-5 Mal am Stück gesendet wird
time.sleep(75)
break
if os.system("ping -c 1 " + ip1) == 0:
offCountIP1 = 0
else:
offCountIP1 += 1
if os.system("ping -c 1 " + ip2) == 0:
offCountIP2 = 0
else:
offCountIP2 += 1
else:
# NAS ist offline
while True:
time.sleep(10)
wDay = time.strftime("%w")
now = time.strftime("%H:%M:%S")
if (now > "05:45:00" and now < "22:00:00" and wDay > "0" and wDay < "6") or (now > "08:30:00" and now < "22:00:00" and (wDay == "0" or wDay == "6")):
if os.system("ping -c 1 " + ip1) == 0 or os.system("ping -c 1 " + ip2) == 0:
#Log schreiben
writeToLog("Starte NAS\n")
os.system("sudo wakeonlan " + nasMAC)
# Sleep, damit der Wakeonlan-Befehl nicht 4-5 Mal am Stück gesendet wird
time.sleep(75)
break
###
Alles anzeigen