python使用telnet连接交换机并保存到txt文档(后续用正则表达式提取到excl表形成运维的巡检记录)

1.需求分析
1)手动巡检还是美滋滋,不需要动脑
2)因为之前看到文章有大佬用pexpect.spawn()来连接网络设备,然后试了一下,发现要linux系统才行,然后就研究出了这个。
2.telnet交换机(cisco)输入命令并导出:源码实现

def telnetip(ip):
    # 连接Telnet服务器
    tn = telnetlib.Telnet(ip, port=23, timeout=50)
    # 输入登录用户名
    fout=open('text1111.txt','w')
    tn.logfile=fout
    tn.read_until(b'Username:')
    tn.write(username + b'\n')

    # 输入登录密码
    # tn.read_until('Password:')
    tn.write(password + b'\n')
    time.sleep(1)
    tn.write(b'show memory' + b'\n')  # 查看内存
    tn.write(b' ' + b'\n')#敲空格,不解释
    tn.write(b' ' + b'\n')
    tn.write(b'show processes cpu sorted' + b'\n')   #查看CPU
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b'show environment' + b'\n')   #查看温度
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b'show ip routing' + b'\n')   #查看路由表
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b'show ip interface brief' + b'\n')   #查看端口状态
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b'show version' + b'\n')  #查看系统版本 
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    tn.write(b' ' + b'\n')
    
    #tn.write(b'domain paoa' + b'\n')
    #tn.write(b'****' + b'\n')
    #tn.write(b'****' + b'\n')
    time.sleep(50)
    result1 = tn.read_very_eager()  # 获得结果
    print(result1)
    result1=str(result1)
    fout.write(result1)
    # 命令执行完毕后,终止Telnet连接(或输入exit退出)
    tn.close()  # 

if __name__ == '__main__':
    # 配置选项
    ip = '172.17.0.1'  # Telnet交换机IP
    username = b'admin'  # 登录用户名
    password = b'p@ss0word'  # 登录密码
    telnetip(ip)

3.用正则表达式提取txt文档里的关键信息,输出到xls

import datetime
import xlwt
from xlwt import *
import re

today=datetime.date.today().strftime('%Y%m%d')
txt='text1111.txt'
file=open(txt,'r+')

listlist=file.readlines()
listlist=str(listlist)  #TypeError: expected string or bytes-like object  因为正则表达式不支持,所以需要转换为字符串
#print(listlist)

time11=''
environment11=''
cpu11=''
memory11=''
briefa11=''
routingtable11=''


#使用正则表达式提取数据
if 'memory' in listlist:
    memory11=re.search('total.*?\s(.*?)\sused',listlist)#内存
    print(memory11.group(1))
if 'five minutes' in listlist:
    cpu11=re.search('\sfive\sminutes.*?\s(\d+%)',listlist)#cpu使用率 
    print(cpu11.group(1))
if 'environment' in listlist:
    environment11=re.search('CPU\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s(.*?)\s',listlist)#运行温度
    print(environment11.group(1))
if 'Uptime' in listlist:#运行时间
    time11=re.search('\s((\d)\sweek,\s(\d)\sdays,\s(\d+)\shours,\s(\d+)\sminutes)',listlist)#运行时间
    print(time11.group())
if 'routing' in listlist:
    routingtable11='NO'
    print(routingtable11)
if 'YES unset  up' in listlist:
    briefa11=re.search('YES\sunset\s\s(up)',listlist)#端口
    print(briefa11.group(1))


workbook = xlwt.Workbook()#创建表格
style = XFStyle()#初始化样式,此样式包含了单元格背景颜色和单元格边框两个属性。
pattern = Pattern()
pattern.pattern = Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = Style.colour_map['red'] #设置单元格背景色为红色
style.pattern = pattern
borders = xlwt.Borders()#设置表格的边框,1是默认实线黑色。
borders.left = 1
borders.right = 1
borders.top = 1
borders.bottom = 1
style.borders = borders


style1 = XFStyle()#只有边框
borders = xlwt.Borders()
borders.left = 1
#borders.left = xlwt.Borders.THIN
borders.right = 1
borders.top = 1
borders.bottom = 1
style1.borders = borders

style3 = XFStyle()#初始化样式,带边框和表格内容居中。
borders = xlwt.Borders()
borders.left = 1
#borders.left = xlwt.Borders.THIN
borders.right = 1
borders.top = 1
borders.bottom = 1
style3.borders = borders
al = xlwt.Alignment()
al.horz = 0x02      # 设置水平居中
al.vert = 0x01      # 设置垂直居中
style3.alignment = al

F51FSwitch = workbook.add_sheet('A4F-4507',cell_overwrite_ok=True)#创建表格的某一分页

first_col=F51FSwitch.col(0)#设置0、1、2、3列的列宽
sec_col=F51FSwitch.col(1)
thr_col=F51FSwitch.col(2)
for_col=F51FSwitch.col(3)
first_col.width=150*25
sec_col.width=100*25
thr_col.width=120*25
for_col.width=320*25

F51FSwitch.write_merge(1,11,0,0,'A4F-4507',style3)#合并单元格(1,11为行1到11行 0,0为列0到0),填入内容
#F51FSwitch.write_merge(1,10,0,1,'QCMC-F3-1FA')#合并0到1列,1到10行
F51FSwitch.write_merge(1,11,1,1,'172.17.0.1',style3)#添加style3的样式,只能填写一个,所以初始化样式的时候根据需求添加多个属性。
F51FSwitch.write(0,0,'设备名称',style)
F51FSwitch.write(0,1,'管理地址',style)
F51FSwitch.write(0,2,'检查项',style)
F51FSwitch.write(0,3,'检查结果',style)
F51FSwitch.write(1,2,'设备状态',style1)
F51FSwitch.write(2,2,'运行时间',style1)
F51FSwitch.write(3,2,'运行温度',style1)
F51FSwitch.write(4,2,'风扇A状态',style1)
F51FSwitch.write(5,2,'风扇B状态',style1)
F51FSwitch.write(6,2,'CPU使用率',style1)
F51FSwitch.write(7,2,'内存使用率',style1)
F51FSwitch.write(8,2,'聚合口A',style1)
F51FSwitch.write(9,2,'聚合口B',style1)
F51FSwitch.write(10,2,'日志条目',style1)
F51FSwitch.write(11,2,'路由条目',style1)

#F51FSwitch.write(1,3,power11,style1)#添加抓取的字符串到相应的表格
F51FSwitch.write(2,3,time11.group(1),style1)
F51FSwitch.write(3,3,environment11.group(1),style1)
#F51FSwitch.write(4,3,fana11,style1)
#F51FSwitch.write(5,3,fanb11,style1)
F51FSwitch.write(6,3,cpu11.group(1),style1)

F51FSwitch.write(7,3,memory11.group(1),style1)

#F51FSwitch.write(8,3,briefa11,style1)
#F51FSwitch.write(9,3,briefb11,style1)
#F51FSwitch.write(10,3,log11,style1)
F51FSwitch.write(11,3,routingtable11,style1)


print ('创建excel文件完成!')
workbook.save('today.xls')

这里创建表格的代码块,是参考了谢冰棍大佬的例子。不同的地方在于正则表达式提取信息的地方

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值