fix: loop hapus address-list sampai bersih sebelum add baru
This commit is contained in:
@@ -76,19 +76,40 @@ def deploy_via_rest_api(router, rsc_filepath="routing-lokal.rsc"):
|
|||||||
session.auth = (user, password)
|
session.auth = (user, password)
|
||||||
session.verify = False
|
session.verify = False
|
||||||
|
|
||||||
# 1. Menghapus list lama di router
|
# 1. Menghapus list lama di router (loop sampai benar-benar bersih)
|
||||||
print(f"Menghapus address-list '{list_name}' lama di router...")
|
print(f"Menghapus address-list '{list_name}' lama di router...")
|
||||||
del_payload = {"script": f"/ip/firewall/address-list/remove [find list=\"{list_name}\"]"}
|
del_payload = {"script": f"/ip/firewall/address-list/remove [find list=\"{list_name}\"]"}
|
||||||
try:
|
|
||||||
res_del = session.post(f"{api_url}/execute", json=del_payload, timeout=30)
|
for attempt in range(10):
|
||||||
if res_del.status_code not in (200, 201):
|
try:
|
||||||
print(f"Warning saat menghapus list (abaikan jika baru): {res_del.text}")
|
session.post(f"{api_url}/execute", json=del_payload, timeout=60)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning execute remove list: {e}")
|
print(f" Warning execute remove: {e}")
|
||||||
|
|
||||||
# Beri waktu luang agar RouterOS mengeksekusi penghapusan di background
|
import time
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
|
# Verifikasi: cek apakah sudah benar-benar kosong
|
||||||
|
try:
|
||||||
|
res_check = session.get(f"{api_url}/ip/firewall/address-list",
|
||||||
|
params={"list": list_name, ".proplist": ".id"}, timeout=10)
|
||||||
|
remaining = len(res_check.json()) if res_check.status_code == 200 else -1
|
||||||
|
except:
|
||||||
|
remaining = -1
|
||||||
|
|
||||||
|
if remaining == 0:
|
||||||
|
print(f" Address-list '{list_name}' bersih (percobaan {attempt + 1})")
|
||||||
|
break
|
||||||
|
elif remaining > 0:
|
||||||
|
print(f" Sisa {remaining} entry, menghapus ulang...")
|
||||||
|
else:
|
||||||
|
print(f" Gagal verifikasi, lanjut...")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print(f" Warning: address-list mungkin belum sepenuhnya bersih setelah 10 percobaan")
|
||||||
|
|
||||||
import time
|
import time
|
||||||
time.sleep(2)
|
time.sleep(1)
|
||||||
|
|
||||||
# 2. Add IP lewat eksekusi Batch Script (Mencegah Payload Too Large)
|
# 2. Add IP lewat eksekusi Batch Script (Mencegah Payload Too Large)
|
||||||
print(f"Mengirim {len(ips)} address ke router secara batch... (±500 list per request)")
|
print(f"Mengirim {len(ips)} address ke router secara batch... (±500 list per request)")
|
||||||
|
|||||||
Reference in New Issue
Block a user