# coding = utf-8
from telnetlib import Telnet
import re
from datetime import datetime
import time, os
# from getpass import getpass
#now_time = str(time.localtime().tm_year) + str(time.localtime().tm_mon) + str(time.localtime().tm_mday) # 获取当前日期时间
now = datetime.now()
now_time1 = str(now.year) + str(now.month) + str(now.day) # 获取当前日期时间
#print(type(now_time))
username = input('username:')
password = input('passwd:')
ip_add_file = open('../测试代码/ip_add.txt') # 打开ip地址文件,需要备份的设备IP地址
path = r'D:\网络配置文件备份\\' + now_time1 # 配置文件保存路径
isExists = os.path.exists(path) # 判断文件是否已经创建
if not isExists:
os.makedirs(path) # 如果没有创建,则新建文件夹
print(path + ' 创建成功!')
else:
print(path + ' 目录已存在') # 已经创建提示
#File_Dir_Info = os.getcwd() + '\\'+'网络配置备份'
# f = os.path.exists('D:\\网络设备备份' + now_time)
#os.makedirs(File_Dir_Info + '/' + now_time) # 创建目录 “/” 为 makedirs创建多级目录
for line in ip_add_file.readlines(): # for循环读取设备IP地址
ip = line.strip()
tn = Telnet(ip, port=23, timeout=10) # 远程连接设备
print('成功登陆设备:' + ip)
tn.read_until(b"Username:") # 读取输出的账户提示信息
tn.write(username.encode('ascii') + b'\n') # 输入账户
tn.read_until(b"Password:") # 读取输出的密码提示信息
tn.write(password.encode('ascii') + b'\n') # 输入密码
# tn.write('system-view'.encode('ascii') + b'\n')
#time.sleep(0.5)
tn.write('screen-length 0 temporary '.encode('ascii') + b'\n') # 设置回显内容不分屏显示
time.sleep(0.5)
tn.write('dis cu '.encode('ascii') + b'\n') # 查询配置信息
time.sleep(3) # 等待结果输出时间,配置越多,输出时间应对应增加
command_result = tn.read_very_eager().decode('ascii')
# result_list = []
# # 循环获取配置文件
# while (True):
# command_result = tn.read_very_eager().decode('ascii')
# result_list.append(command_result)
# if '---- More ----' in command_result.strip():
# tn.write(b" ")
# time.sleep(1)
# else:
# break
# result_str = '\n'.join(result_list)
# sw_name = re.findall('<(\w.*\d)>', result_str)[0]
#Sw_name = re.findall('<(\w+\_\d\w\_\w+\_\w+)>', result_str)[0] # 提取配置文件中的设备名称
Sw_name = re.findall('<(\w+\_\d\w\_\w+\_\w+)>', command_result)[0] # 提取配置文件中的设备名称
#Sw_Config_File = open(File_Dir_Info + '\\' + now_time + '\\' + Sw_name + '-' + ip + '.txt', mode='a+') # 备份配置文件保存路径+文件名
backup = open(path + '\\' + Sw_name + '-' + ip + '.txt','a+') # 备份配置文件保存路径+文件名
#backup.write(result_str)
backup.write(command_result) # 将回显内容写入backup这个对象,相当于写入了备份文件中
#Sw_Config_File.write(result_str)
#print(result_str)
#print(command_result) # 打印回显结果
tn.close()
# coding = utf-8
from telnetlib import Telnet
import re
from datetime import datetime
import time, os
# from getpass import getpass
#now_time = str(time.localtime().tm_year) + str(time.localtime().tm_mon) + str(time.localtime().tm_mday) # 获取当前日期时间
now = datetime.now()
now_time1 = str(now.year) + str(now.month) + str(now.day) # 获取当前日期时间
username = input('username:')
password = input('passwd:')
ip_add_file = open(os.getcwd() + 'ip_add.txt') # 打开ip地址文件,需要备份的设备IP地址txt文件
path = r'D:\网络配置文件备份\\' + now_time1 # 配置文件保存路径
isExists = os.path.exists(path) # 判断文件是否已经创建
if not isExists:
os.makedirs(path) # 如果没有创建,则新建文件夹
print(path + ' 创建成功!')
else:
print(path + ' 目录已存在') # 已经创建提示
for line in ip_add_file.readlines(): # for循环读取设备IP地址
ip = line.strip()
tn = Telnet(ip, port=23, timeout=10) # 远程连接设备
print('成功登陆设备:' + ip)
tn.read_until(b"Username:") # 读取输出的账户提示信息
tn.write(username.encode('ascii') + b'\n') # 输入账户
tn.read_until(b"Password:") # 读取输出的密码提示信息
tn.write(password.encode('ascii') + b'\n') # 输入密码
# tn.write('system-view'.encode('ascii') + b'\n')
#time.sleep(0.5)
tn.write('screen-length 0 temporary '.encode('ascii') + b'\n') # 设置回显内容不分屏显示
time.sleep(0.5)
tn.write('dis cu '.encode('ascii') + b'\n') # 查询配置信息
time.sleep(3) # 等待结果输出时间,配置越多,输出时间应对应增加
command_result = tn.read_very_eager().decode('ascii')
time.sleep(0.5)
Sw_name = re.findall('<(\w+\_\d\w\_\w+\_\w+)>', command_result)[0] # 提取配置文件中的设备名称
backup = open(path + '\\' + Sw_name + '-' + ip + '.txt','a+') # 备份配置文件保存路径+文件名
print("成功备份:" + Sw_name + '-' + ip)
backup.write(command_result) # 将回显内容写入backup这个对象,相当于写入了备份文件中
backup.close()
tn.close()
print('备份完成!')