47 lines
991 B
Python
47 lines
991 B
Python
import asyncio
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import time
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import tapo
|
|
from dotenv import load_dotenv
|
|
from scapy.all import ICMP, IP, sr
|
|
|
|
load_dotenv()
|
|
|
|
tapo_username = os.getenv("TAPO_USERNAME")
|
|
tapo_password = os.getenv("TAPO_PASSWORD")
|
|
tapoip = os.getenv("TAPO_IP")
|
|
|
|
|
|
async def powercycle():
|
|
tapoapi = tapo.ApiClient(tapo_username, tapo_password)
|
|
tapoplug = await tapoapi.p100(tapoip)
|
|
await tapoplug.off()
|
|
await asyncio.sleep(1)
|
|
await tapoplug.on()
|
|
|
|
|
|
asyncio.run(powercycle())
|
|
|
|
print("sleeping for 80s, then ping until up")
|
|
|
|
time.sleep(80)
|
|
|
|
while True:
|
|
ans, unans = sr(IP(dst="192.168.42.6") / ICMP(), verbose=False)
|
|
if len(ans) > 0:
|
|
break
|
|
|
|
print("Collecting :")
|
|
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + "-powercut"
|
|
log_dir = Path("./logs") / timestamp
|
|
log_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
shutil.copy(Path(sys.argv[0]), log_dir / sys.argv[0])
|
|
|
|
print(" - [x] script.py")
|