Skip to content

Commit

Permalink
πŸ•Š
Browse files Browse the repository at this point in the history
πŸ•Š
  • Loading branch information
keyiflerolsun committed Sep 13, 2023
1 parent d0c692e commit 3bf3617
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
70 changes: 62 additions & 8 deletions KeeneticPy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Bu araç @keyiflerolsun tarafından | @KekikAkademi için yazılmıştır.

from requests import Session
from hashlib import md5, sha256
from Kekik.cli import konsol
from requests import Session
from hashlib import md5, sha256
from zipfile import ZipFile, ZIP_DEFLATED
from os import remove, listdir, path
from datetime import datetime
from Kekik import slugify

class Keenetic:
def __init__(self, kullanici:str="admin", sifre:str="", ip:str="192.168.1.1"):
Expand Down Expand Up @@ -74,15 +79,15 @@ def global_ip(self) -> dict:
veri = self.__oturum.post(
url = f"{self.__rci}",
json = {
"ip" : {"http":{"ssl":{"acme":{"list":{}}}}},
"show" : {"clock":{"date":{}},"schedule":{},"internet":{"status":{}},"version":{},"system":{},"interface":{},"ip":{"name-server":{},"route":{},"hotspot":{"details":"wireless"}},"rc":{"interface":{},"service":{},"user":{},"components":{"auto-update":{}},"ip":{"http":{}},"dlna":{}},"ndns":{},"acme":{},"dyndns":{},"ping-check":{},"cifs":{},"printers":{},"ipv6":{"addresses":{},"prefixes":{},"routes":{}},"dlna":{},"usb":{},"media":{}},
"ls" : {},
"whoami": {}
"ip" : {"http":{"ssl":{"acme":{"list":{}}}}},
"show" : {"clock":{"date":{}},"schedule":{},"internet":{"status":{}},"version":{},"system":{},"interface":{},"ip":{"name-server":{},"route":{},"hotspot":{"details":"wireless"}},"rc":{"interface":{},"service":{},"user":{},"components":{"auto-update":{}},"ip":{"http":{}},"dlna":{}},"ndns":{},"acme":{},"dyndns":{},"ping-check":{},"cifs":{},"printers":{},"ipv6":{"addresses":{},"prefixes":{},"routes":{}},"dlna":{},"usb":{},"media":{}},
"ls" : {},
"whoami" : {}
}
).json()

for ls in veri["ls"]["entry"].copy().keys():
if ls in ['flash:', 'temp:', 'proc:', 'sys:', 'log', 'running-config', 'startup-config', 'default-config', 'ndm:', 'debug:', 'storage:']:
if ls in ["flash:", "temp:", "proc:", "sys:", "log", "running-config", "startup-config", "default-config", "ndm:", "debug:", "storage:"]:
del veri["ls"]["entry"][ls]

return {
Expand All @@ -93,4 +98,53 @@ def global_ip(self) -> dict:
# "dlna" : veri["show"]["dlna"]["directory"],
"ipv4" : veri["show"]["interface"]["PPPoE0"]["address"],
"ipv6" : [adres for adres in veri["show"]["ipv6"]["addresses"]["address"] if adres["interface"] == "PPPoE0"][0]["address"],
}
}

def __zip_tarih_al(self, dosya_adi:str) -> datetime:
tarih_str = dosya_adi.split("_")[-1].replace(".zip", "")
return datetime.strptime(tarih_str, "%d-%m-%Y")

def backup(self, maksimum_yedek=5):
if not self._yetki:
assert False, "Yetkisiz Erişim."

tarih = datetime.now().strftime("%d-%m-%Y")
cihaz = self.version()
zip_dosyasi = f"{slugify(cihaz['description'])}_{slugify(cihaz['hw_version'])}_{tarih}.zip"
fw = "firmware.bin"
config = "startup-config.txt"

fw_istek = self.__oturum.get(f"{self.__panel}/ci/firmware")
if fw_istek.status_code != 200:
konsol.log(f"[red][!] {fw} Δ°ndirme HatasΔ±: {fw_istek.status_code}")
else:
with open(fw, "wb") as dosya:
dosya.write(fw_istek.content)
konsol.log(f"[green][+] {fw} indirildi!")

config_istek = self.__oturum.get(f"{self.__panel}/ci/startup-config")
if config_istek.status_code != 200:
konsol.log(f"[red][!] {config} Δ°ndirme HatasΔ±: {config_istek.status_code}")
else:
with open(config, "wb") as dosya:
dosya.write(config_istek.content)
konsol.log(f"[green][+] {config} indirildi!")

with ZipFile(zip_dosyasi, "w", ZIP_DEFLATED) as arsiv:
arsiv.write(fw)
arsiv.write(config)

remove(config)
remove(fw)

yedekler = sorted(
[dosya for dosya in listdir() if dosya.endswith(".zip") and dosya.startswith(zip_dosyasi.split("_")[0])],
key = self.__zip_tarih_al,
reverse = True
)

for yedek in yedekler[maksimum_yedek:][::-1]:
remove(yedek)
konsol.log(f"[yellow][~] {yedek} dosyasΔ± silindi!")

konsol.log(f"[green][+] {zip_dosyasi} başarıyla oluşturuldu!")
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ from KeeneticPy import Keenetic

hero = Keenetic(sifre="cokomellisifre")

hero.backup(maksimum_yedek=2)

konsol.print(hero.system())
konsol.print(hero.version())

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup(
# ? Genel Bilgiler
name = "KeeneticPy",
version = "0.4",
version = "0.5",
url = "https://github.com/keyiflerolsun/KeeneticPy",
description = "Python Lib for Keenetic Routers",
keywords = ["KeeneticPy", "KekikAkademi", "keyiflerolsun"],
Expand All @@ -28,6 +28,7 @@
"pip",
"setuptools",
"wheel",
"Kekik",
"requests"
],

Expand Down

0 comments on commit 3bf3617

Please sign in to comment.