放了三天假,睡觉睡觉睡觉。
乙方维保厂商的工程师经常要执行与甲方维保合同里的巡检任务,
论甲方设备数量,消耗时间也有多有少,
乙方工程师在现场处理问题的时间非常宝贵。
拿来做这类重复的工作,代价太高。
写点代码完全可以让这类重复的工作自动完成,腾出时间做更重要的事情,比如和客户吹牛逼。
代码流程:
test.png主机信息文件:
./conf/host_info.ini
# host information
# host username password port type
127.0.0.1 neo 123456 22 test_ubuntu
192.168.0.23 root 123456 22 test_centos
#192.168.1.10 admin 123456 22 sw_l2
#192.168.1.11 admin 123456 22 sw_l3
#192.168.1.12 admin 123456 22 route
#192.168.1.13 admin 123456 22 fw
#192.168.1.14 admin 123456 22 nexus
各类设备手机信息命令:
./conf/sw_l2.conf
系统版本#show ver
接口信息#show ip inter br
Flash卡#show flash
=====
./conf/test_ubuntu.conf
内存#free -m
磁盘#df -h
ip信息#ip a
开放端口#ss -tnl
ESTAB数量#ss -al | grep ESTAB | wc -l
SYN-SENT#ss -al | grep SYN-SENT | wc -l
防火墙规则#iptables -nL
代码运行文件:
# 运行 python3.5 Regular_visit.py
#!/bin/python3.5
# -*- coding:utf-8 -*-
# author: neo
# date: 2017-05-31
import xlwt
import paramiko
import os
import sys
def header(work_sheet):
work_sheet.write(0, 0,'检查主机',style0)
work_sheet.write(0, 1, '项目', style0)
work_sheet.write(0, 2, '执行命令', style0)
work_sheet.write(0, 3, '检查结果', style0)
return work_sheet
def run(text):
for line in text.readlines():
if line[0:1] == '#':continue
items = line.split()
run_command(items)
def run_command(item):
host,user,passwd,port,type = item
try:
cmds = open('./conf/'+item[4]+'.conf','r',encoding='utf-8')
except:
print ("Command conf error,please check cfg file.",item[4])
exit(1)
work_sheet = work_book.add_sheet(item[0])
work_sheet = header(work_sheet)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host,int(port),user,passwd,timeout=5)
except:
print ("ssh connect timeout,please check network connect.",item[0])
for cmd in cmds.readlines():
rows = len(work_sheet.rows)
items = cmd.split('#')
check_tye = items[0].encode('utf-8')
command = items[1].encode('utf-8')
stdin, stdout, stderr = ssh.exec_command(command.decode('utf-8'))
return_info = stdout.read().strip()
work_sheet.write(rows,0,host)
work_sheet.write(rows,1,items[0])
work_sheet.write(rows,2,items[1])
work_sheet.write(rows,3,return_info.decode('utf-8'))
print ('host command success :',host)
cmds.close()
if __name__ == '__main__':
style0 = xlwt.easyxf('pattern: pattern solid,fore_colour yellow;' +
'font: name Times New Roman, color-index black, bold on;' +
'borders: left thick, right thick, top thick, bottom thick;' +
'align: horiz center ',
num_format_str='0,000.00')
work_book = xlwt.Workbook(encoding='utf-8')
host_info = open('./conf/host_info.ini','r',encoding='utf-8')
run(host_info)
work_book.save('check.xls')
host_info.close()
测试效果:
文件.png 信息收集.png所以每个客户只需要将主机文件信息修改,对每类维护设备的命令修改,就可以简简单单完成巡检工作了。
网友评论