python利用paramiko實現交換機巡檢的示例
直接上代碼
#-*- coding:UTF-8 -*-import paramikoimport timestarttime = time.strftime(’%Y-%m-%d %T’)start_info = '巡檢開始時間:'+str(starttime)cmd_filepath = r'd:Pythonpyxunjiancmd.txt'cmd_file = open(cmd_filepath,'r')cmds = cmd_file.readlines()dev_filepath = r'd:Pythonpyxunjiandevice_info.txt'dev_file = open(dev_filepath,'r')while 1: dev_info = dev_file.readline() if not dev_info : break else : devs = dev_info.split(’,’) ip = devs[0] username = devs[1] password = devs[2].strip() password = password.strip(’n’) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname = ip,username = username,password = password) print('成功連接',ip) command = ssh.invoke_shell() time.sleep(3) command.send(’Nn’) #該行非必須 command.send(’screen-length 0 temporaryn’) #取消分屏顯示 for cmd in cmds: command.send(cmd+’n’) time.sleep(5) output = command.recv(65535).decode() log = open(r'd:Pythonpyxunjian'+ip+'.txt',’a’) endtime = time.strftime(’%Y-%m-%d %T’) end_info = '巡檢結束時間:'+str(endtime) log.write(start_info+’nn’+output+’nn’+end_info) log.close()dev_file.close()#巡檢命令文檔cmd.txtdisplay devicedisplay environmentdisplay alarm urgendisplay memory-usagedisplay cpu-usagedisplay logbuffer level 0display logbuffer level 1display logbuffer level 2display logbuffer level 3display logbuffer level 4#設備信息 device_info.txt192.168.10.11,admin,[email protected],admin,[email protected],admin,[email protected],admin,Huawei@123
以上就是python利用paramiko實現交換機巡檢的示例的詳細內容,更多關于paramiko交換機巡檢的資料請關注好吧啦網其它相關文章!
相關文章: