java中的接口是类吗
1740
2022-09-12
python 自动化运维脚本,可以巡检思科,华三,等网络设备(python代码大全)
python 自动化运脚本
按照oxidized搭建的CMDB 为数据源。自动向数据库读取账号密码,设备名称,等 并输出状态信息 如图所示
数据库字段如下 enable 字段存放思科的enable密码,login 是登陆方式 存放和telnet等登陆方式。device 是设备类型。
第一段代码 批量抓取思科,华三的配置,命令部分我已经做了注释(具体模块使用可以百度)
可以根据此脚本更改命令列表,比如批量更改网络设备密码,批量备份配置到ftp,批量开启snmp,批量指定syslog服务器,批量设置ntp ,批量............
#!/usr/bin/python #-*- coding: utf-8 -*- import paramiko import threading import time import os import pymysql def ssh2(ip,username,passwd,cmd,name): try: log = open ('/zhichang/xunjian/'+'%s-%s.txt'%(name,ip),'wb+') #日志保存地点 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,22,username,passwd,timeout=5) ssh_shell = ssh.invoke_shell() #以执行多条命令 print ssh_shell.recv(1024) for m in cmd: commd = ssh_shell.sendall(m) #列表循环执行 time.sleep(1) log.write( ssh_shell.recv(999999)) ssh.close() except : print '%s\tError\n'%(ip) if __name__=='__main__': h3c_sw = ["screen-length disable \n" , "display ip routing-table \n ", "dis int \n" , "display cpu-usage \n", "display memory summary \n", "display fan \n","display power \n","display environment \n","display version \n"] #华三交换机巡检 \n 是模拟回车 h3c_wifi = ["screen-length disable \n" ,"display ip routing-table \n ","dis int \n" ,"display cpu-usage \n","display memory summary \n", "display wlan ap all \n","display wlan client \n","display version \n"]#华三wifi 巡检 cisco_2911 = ["enable \n","","terminal length 0 \n","show processes cpu \n","show processes memory \n","show ip route summ \n ","show version \n ","show int \n " ]# 思科设备巡检 thread=[] print "Begin......" db = pymysql.connect( host="10.50.99.247", user="数据库账号", passwd="数据库密码", port=3306, db="network", charset='utf8') cursor = db.cursor() cursor.execute("select ipadd,username,password,device,name from net_dev where model='comware' ") #华三设备信息抓取 h3c_sql = cursor.fetchall() for i in h3c_sql: if i[3] == "WX2500H" : #进行逻辑判断 a=threading.Thread(target=ssh2, args=(i[0], i[1], i[2], h3c_wifi, i[4])) #多线程用列表位置锚定 参数传递 a.start() else : a = threading.Thread(target=ssh2, args=(i[0], i[1], i[2], h3c_sw, i[4])) a.start() cursor.execute("select ipadd,username,password,device,enable,name from net_dev where model='ios' and `group`='世贸职场'")#思科设备信息抓取 cisco_sql = cursor.fetchall() for i in cisco_sql : if i[3] == "cisco2911": cisco_2911[1] = str(i[4]+ '\n') #由于思科enable 密码是从数据库读取,需要对列表进行修改, b = threading.Thread(target=ssh2, args=(i[0], i[1], i[2], cisco_2911, i[5])) b.start() b.join() #由于enabel变量的存在,多线程执行变量传递会有问题,用join进行锁定 db.close()
此python文件执行完成后 生成的log会保存到本地。
用正则表达式 读取 参数进行输出 只输出了华三设备
#!/usr/bin/python #-*- coding: utf-8 -*- import re import os import sys import pymysql def xunjian(ip,name) : try : with open ('/zhichang/xunjian/%s-%s.txt'%(name,ip),'r') as file : #批量打开文件 message = file.read() pattern = r'uptime.*minutes' #正则表达式匹配 match = re.findall(pattern,message) print (ip+':设备运行时间'+str(match)) routes = re.findall(r'Routes : \d+',message) print (ip+':路由条数'+str(routes)) cpu = re.findall(r'\d+.*in last 5 minutes',message) print(ip+':cpu使用率'+cpu[0]) memory = re.findall(r'FreeRatio\s+.*%\s',message) memory1= re.findall(r'\d{2}.\d+%',memory[0]) print (ip+':内存使用率'+memory1[0]) power = re.findall(r'--\s+2\s+\w+',message) power = re.findall(r'[a-zA-Z]+',power[0]) print (ip+':电源状态'+power[0]) fan = re.findall(r' State\s+:\s+[a-zA-Z]+',message) fan = re.findall(r'[a-zA-Z]+$',fan[0]) print (ip+':风扇状态'+fan[0]) print ('-------------------------------------------------------\n-------------------------------------------------------\n-------------------------------------------------------\n-------------------------------------------------------') except: pass if __name__=='__main__': db = pymysql.connect( host="10.50.99.247", user="network", passwd="xxxx", port=3306, db="network", charset='utf8') cursor = db.cursor() cursor.execute("select ipadd,name from net_dev where model='comware' ") data = cursor.fetchall() for i in data: xunjian(str(i[0])) db.close()
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~