Скрипт обработки файла реестра запрещенных сайтов Роскомнадзора
#!/usr/bin/python3.1
#Модули
import telnetlib
import getpass
import time
import base64
import os
from xml.dom.minidom import *
import logging
LOG_FILE = '/tcpdumps/rkn/finish.log'
logging.basicConfig(level = logging.DEBUG, filename = LOG_FILE, format='%(asctime)s %(message)s')
#Парсим файл
xml = parse('/tcpdumps/rkn/dump.xml')
ip = xml.getElementsByTagName('ip')
f = open('/tcpdumps/rkn/action.txt', 'w')
for node in ip:
f.write(node.childNodes[0].nodeValue + '\n')
f.close()
# Сообщение отладочное
logging.debug( 'Файл обработан' )
#Сессия telnet
tn = telnetlib.Telnet("192.168.44.16", 23)
#Логин и пароль
tn.read_until(b"Username:")
tn.write(b'XXXX\r')
tn.read_until(b"Password:")
tn.write(b'XXXX\r')
tn.read_until(b"c7206>")
tn.write(b'en\r')
tn.read_until(b"Password:")
tn.write(b'XXXX\r')
#Снимаем старый лист с интерфейса и убираем его из конфига
tn.read_until(b"c7206#")
tn.write(b'configure terminal\r')
tn.read_until(b"c7206(config)#")
tn.write(b'interface GigabitEthernet 0/3.141\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'no ip access-group to_INET out\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'exit\r')
tn.read_until(b"c7206(config)#")
tn.write(b'interface GigabitEthernet 0/2.315\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'no ip access-group to_INET out\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'exit\r')
tn.read_until(b"c7206(config)#")
tn.write(b'no ip access-list extended to_INET\r')
tn.read_until(b"c7206(config)#")
tn.write(b'ip access-list extended to_INET\r')
# Сообщение отладочное
logging.debug( 'Сессия подключена, старые записи стерты' )
#Адреса для блока
#Чтение из файла, обработка данных, преобразование и декодирование
fr = open('/tcpdumps/rkn/action.txt', 'r')
for line in fr:
L = str(line)
L1 = len(line) - 1
L2 = line[:L1]
L3 = 'deny ip any host ' + L2 + '\r'
s = L3
b = s.encode()
c = base64.b64encode(b)
b1 = base64.b64decode(c)
#print(b1)
tn.read_until(b"c7206(config-ext-nacl)#")
tn.write(b1)
tn.read_until(b"c7206(config-ext-nacl)#")
tn.write(b'permit ip any any\r')
tn.read_until(b"c7206(config-ext-nacl)#")
tn.write(b'exit\r')
#Навешиваем новый лист на интерфейс
tn.read_until(b"c7206(config)#")
tn.write(b'interface GigabitEthernet 0/3.141\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'ip access-group to_INET out\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'exit\r')
tn.read_until(b"c7206(config)#")
tn.write(b'interface GigabitEthernet 0/2.315\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'ip access-group to_INET out\r')
tn.read_until(b"c7206(config-subif)#")
tn.write(b'exit\r')
# Сообщение отладочное
logging.debug( 'Новые записи добавлены' )
#Выходим и закрываем сессию
tn.read_until(b"c7206(config)#")
tn.write(b'exit\r')
tn.read_until(b"c7206#")
tn.write(b'wr\r')
tn.read_until(b"c7206#")
tn.write(b'exit\r')
tn.close()
#Удаляем файл
os.remove('/tcpdumps/rkn/dump.xml')
# Сообщение отладочное
logging.debug( 'Сессия закрыта, старый файл удален' )
Комментарии пользователей
Анонимам нельзя оставоять комментарии, зарегистрируйтесь!