Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. import telnetlib
  2. import time
  3. import datetime
  4. import csv
  5.  
  6.  
  7. def parse_csv(filename):
  8.     with open(filename, encoding='utf-8') as f:        
  9.         next(f)  # пропускаем заголовки
  10.         yield from csv.reader(f)
  11.  
  12. # если файл небольшой 100-1000 строк
  13. routers = list(parse_csv('routers.csv'))
  14.  
  15.  
  16. while len(routers) > 0:
  17.     adress, WANip, VPNip, user, password = routers.pop(0)[0].split(';')
  18.     now = datetime.datetime.now()
  19.     nowt = (now.strftime("%d-%m-%Y %H:%M"))
  20.     #print(adress, WANip, VPNip, user, password, now, nowt)
  21.  
  22.     #Автологин
  23.     try:
  24.         tn = telnetlib.Telnet(WANip)
  25.     except Exception as e:
  26.         print(e) # выводим в консоль исключение
  27.         continue
  28.  
  29.     tn.read_until(b"Login: ", timeout=2)
  30.     tn.write(user.encode('ascii') + b"\n")
  31.     tn.read_until(b"Password: ", timeout=2)
  32.     tn.write(password.encode('ascii') + b"\n")
  33.     tn.read_until(b"(config)> ", timeout=1)
  34.  
  35.     #Собираем инфу из роутера
  36.     tn.read_until(b"(config)> ", timeout=2)
  37.     tn.write('show version'.encode('ascii') + b"\n")
  38.     time.sleep(2)
  39.    
  40.     # Сбор модели
  41.     out = tn.read_very_eager().decode('ascii')
  42.     out = out.split(' ')
  43.     if 'description:' in out:
  44.         i1 = out.index('description:')
  45.         model = (out[i1 + 1])+' '+(out[i1 + 2])+' '+(out[i1 + 3]).strip('\r\n\r\n(config)>')
  46.     elif 'device:' in out:
  47.         i2 = out.index('device:')
  48.         model = (out[i2 + 1])+' '+(out[i2 + 2]).strip('\r\n')
  49.     else:
  50.         model = "неизвестная модель"
  51.         i3 = out.index('release:')
  52.         release = (out[i3 + 1]).strip('\r\n')
  53.  
  54.     tn.read_until(b"(config)> ", timeout=2)
  55.     tn.read_until(b"(config)> ", timeout=2)
  56.     tn.write('show interface PPTP0'.encode('ascii') + b"\n")
  57.     time.sleep(2)
  58.  
  59.     # Сбор инфы по VPN
  60.     out1 = tn.read_very_eager().decode('utf-8')
  61.     out1 = out1.split(' ')
  62.     if 'description:' in out1:
  63.         i4 = out1.index('description:')
  64.         des = (out1[i4 + 1]).strip('\r\n')
  65.     else:
  66.         des = 'Подключение VPN не настроено'
  67.  
  68.     # Сбор Серийного номера
  69.     tn.write('show defaults'.encode('ascii') + b"\n")
  70.     time.sleep(6)
  71.     out2 = tn.read_very_eager().decode('ascii')
  72.     out2 = out2.split(' ')
  73.     if 'serial:' in out2:
  74.         i5 = out2.index('serial:')
  75.         sn = (out2[i5 + 1]).strip('\r\n')
  76.     else:
  77.         sn = 'В FW нет serial'
  78.  
  79.     #Сбор MAC Адреса
  80.     tn.read_until(b"(config)> ", timeout=1)
  81.     tn.write('show interface ISP'.encode('ascii') + b"\n")
  82.     time.sleep(2)
  83.     out3 = tn.read_very_eager().decode('ascii')
  84.     out3 = out3.split(' ')
  85.     if 'mac:' in out3:
  86.         i6 = out3.index('mac:')
  87.         mac = (out3[i6 + 1]).strip('\r\n')
  88.     else:
  89.         mac = ''
  90.  
  91.     #Сбор конфига
  92.     tn.write('show running-config'.encode('ascii') + b"\n")
  93.     time.sleep(2)
  94.     out4 = tn.read_very_eager().decode('ascii')
  95.     out4 =out4.split('\r\n')
  96.    
  97.     #Запись данных в файл (Buckup config)
  98.     handle = open("{}.txt".format(WANip).encode('ascii'), "w")
  99.     for l in out4:
  100.         handle.write (l+'\n')
  101.         handle.close()
  102.     #Запись данных в файл (Информация)
  103.     handle = open("information.txt", "a")
  104.     handle.write('\n'+adress+';'+nowt+';'+WANip+';'+VPNip+';'+model+';'+release+';'+sn+';'+mac+';'+des+';'+'\n')
  105.     handle.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement