Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import telnetlib
- import time
- import datetime
- import csv
- def parse_csv(filename):
- with open(filename, encoding='utf-8') as f:
- next(f) # пропускаем заголовки
- yield from csv.reader(f)
- # если файл небольшой 100-1000 строк
- routers = list(parse_csv('routers.csv'))
- while len(routers) > 0:
- adress, WANip, VPNip, user, password = routers.pop(0)[0].split(';')
- now = datetime.datetime.now()
- nowt = (now.strftime("%d-%m-%Y %H:%M"))
- #print(adress, WANip, VPNip, user, password, now, nowt)
- #Автологин
- try:
- tn = telnetlib.Telnet(WANip)
- except Exception as e:
- print(e) # выводим в консоль исключение
- continue
- tn.read_until(b"Login: ", timeout=2)
- tn.write(user.encode('ascii') + b"\n")
- tn.read_until(b"Password: ", timeout=2)
- tn.write(password.encode('ascii') + b"\n")
- tn.read_until(b"(config)> ", timeout=1)
- #Собираем инфу из роутера
- tn.read_until(b"(config)> ", timeout=2)
- tn.write('show version'.encode('ascii') + b"\n")
- time.sleep(2)
- # Сбор модели
- out = tn.read_very_eager().decode('ascii')
- out = out.split(' ')
- if 'description:' in out:
- i1 = out.index('description:')
- model = (out[i1 + 1])+' '+(out[i1 + 2])+' '+(out[i1 + 3]).strip('\r\n\r\n(config)>')
- elif 'device:' in out:
- i2 = out.index('device:')
- model = (out[i2 + 1])+' '+(out[i2 + 2]).strip('\r\n')
- else:
- model = "неизвестная модель"
- i3 = out.index('release:')
- release = (out[i3 + 1]).strip('\r\n')
- tn.read_until(b"(config)> ", timeout=2)
- tn.read_until(b"(config)> ", timeout=2)
- tn.write('show interface PPTP0'.encode('ascii') + b"\n")
- time.sleep(2)
- # Сбор инфы по VPN
- out1 = tn.read_very_eager().decode('utf-8')
- out1 = out1.split(' ')
- if 'description:' in out1:
- i4 = out1.index('description:')
- des = (out1[i4 + 1]).strip('\r\n')
- else:
- des = 'Подключение VPN не настроено'
- # Сбор Серийного номера
- tn.write('show defaults'.encode('ascii') + b"\n")
- time.sleep(6)
- out2 = tn.read_very_eager().decode('ascii')
- out2 = out2.split(' ')
- if 'serial:' in out2:
- i5 = out2.index('serial:')
- sn = (out2[i5 + 1]).strip('\r\n')
- else:
- sn = 'В FW нет serial'
- #Сбор MAC Адреса
- tn.read_until(b"(config)> ", timeout=1)
- tn.write('show interface ISP'.encode('ascii') + b"\n")
- time.sleep(2)
- out3 = tn.read_very_eager().decode('ascii')
- out3 = out3.split(' ')
- if 'mac:' in out3:
- i6 = out3.index('mac:')
- mac = (out3[i6 + 1]).strip('\r\n')
- else:
- mac = ''
- #Сбор конфига
- tn.write('show running-config'.encode('ascii') + b"\n")
- time.sleep(2)
- out4 = tn.read_very_eager().decode('ascii')
- out4 =out4.split('\r\n')
- #Запись данных в файл (Buckup config)
- handle = open("{}.txt".format(WANip).encode('ascii'), "w")
- for l in out4:
- handle.write (l+'\n')
- handle.close()
- #Запись данных в файл (Информация)
- handle = open("information.txt", "a")
- handle.write('\n'+adress+';'+nowt+';'+WANip+';'+VPNip+';'+model+';'+release+';'+sn+';'+mac+';'+des+';'+'\n')
- handle.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement