Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сохранение полученных ip в формате команды для роутеров Mikrotik (ip/firewall/address-list add) #9

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add mikrotik ip/firewall/address-list format.
  • Loading branch information
Shaman2010 committed Aug 11, 2024
commit 047d70478dfa4a67aeead26383488f1b5f60b0df
21 changes: 19 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,13 @@ def check_dns_servers(dns_servers, dns_server_indices):
return selected_dns_servers


def process_file_format(filename, filetype, gateway):
def process_file_format(filename, filetype, gateway, selected_services):
if not filetype:
filetype = input("\n\033[33mВ каком формате сохранить файл?\033[0m"
"\n\033[32mwin\033[0m - route add IP mask MASK GATEWAY"
"\n\033[32munix\033[0m - ip route IP/MASK GATEWAY"
"\n\033[32mcidr\033[0m - IP/MASK"
"\n\033[32mmt-al\033[0m - Mikrotik ip/firewall/address-list add syntax"
"\n\033[32mПустое значение\033[0m - только IP"
"\nВаш выбор: ")

Expand Down Expand Up @@ -281,6 +282,22 @@ def process_file_format(filename, filetype, gateway):
with open(filename, 'w', encoding='utf-8-sig') as file:
for ip in ips:
file.write(f"{ip.strip()}/32\n")
elif filetype.lower() == 'mt-al':
try:
with open(filename, 'r', encoding='utf-8-sig') as file:
ips = file.readlines()
except Exception as e:
print(f"Ошибка чтения файла: {e}")
return

if ips:
address_list_name = input("\nВведите название списка адресов (address-list): ")
selected_service = ','.join(selected_services)
if not address_list_name:
address_list_name = 'address-list-name'
with open(filename, 'w', encoding='utf-8-sig') as file:
for ip in ips:
file.write(f"ip/firewall/address-list add list={address_list_name} comment={selected_service} address={ip.strip()}/32\n")
else:
pass

Expand Down Expand Up @@ -333,7 +350,7 @@ async def main():
print(f"Исключено IP-адресов 'заглушек': {null_ips_count[0]}")
print(f"Разрешено IP-адресов из DNS имен: {len(unique_ips_all_services)}")

process_file_format(filename, filetype, gateway)
process_file_format(filename, filetype, gateway, selected_services)

if run_command:
print("\nВыполнение команды после завершения скрипта...")
Expand Down