Python自動(dòng)巡檢H3C交換機(jī)實(shí)現(xiàn)過(guò)程解析
1.通過(guò)netmiko模塊登錄交換機(jī),協(xié)議ssh,執(zhí)行收集信息命令,保存至txt文件
2.過(guò)濾txt文件中的內(nèi)容,保存到excel,使用xlwt模塊實(shí)現(xiàn)。
3.sendmai發(fā)送excel郵件。或者發(fā)送給釘釘機(jī)器人也可以
4.使用crond定時(shí)發(fā)送巡檢報(bào)表。
代碼如下
#!/usr/bin/python3 #H3c交換機(jī)import timefrom netmiko import ConnectHandlernow = time.strftime('%Y%m%d',time.localtime(time.time()))log_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())ip_list = [ [’sw-001’,’192.168.1.1’], [’sw-002’,’192.168.1.2’],]SW = { ’device_type’:’hp_comware’, ’username’:’admin’, ’ip’:’’, ’password’:'$password'}for ip_item in ip_list: SW[’ip’] = ip_item[1] connect = ConnectHandler(**SW) print(log_time + ’Successfully connected to ’ + ip_item[0]) output = connect.send_command(’system view’) iproute = connect.send_command('display ip routing-table') version = connect.send_command(’dis version’) fan = connect.send_command(’display fan’) cpu = connect.send_command(’display cpu-usage’) mem = connect.send_command(’display memory’) env = connect.send_command(’display environment’) fo = open(’xusj’,’w’) fo.write(iproute) fo.write(fan) fo.write(cpu) fo.write(mem) fo.write(env) fo.close()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 改變 Python 中線程執(zhí)行順序的方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. 詳解Python模塊化編程與裝飾器4. Python下使用Trackbar實(shí)現(xiàn)繪圖板5. html小技巧之td,div標(biāo)簽里內(nèi)容不換行6. Python通過(guò)format函數(shù)格式化顯示值7. python web框架的總結(jié)8. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式9. python使用ctypes庫(kù)調(diào)用DLL動(dòng)態(tài)鏈接庫(kù)10. Python性能測(cè)試工具Locust安裝及使用
