python利用paramiko实现交换机巡检的示例

时间:2021-05-22

直接上代码

#-*- coding:UTF-8 -*-import paramikoimport timestarttime = time.strftime('%Y-%m-%d %T')start_info = "巡检开始时间:"+str(starttime)cmd_filepath = r"d:\Python\py\xunjian\cmd.txt"cmd_file = open(cmd_filepath,"r")cmds = cmd_file.readlines()dev_filepath = r"d:\Python\py\xunjian\device_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('N\n') #该行非必须 command.send('screen-length 0 temporary\n') #取消分屏显示 for cmd in cmds: command.send(cmd+'\n') time.sleep(5) output = command.recv(65535).decode() log = open(r"d:\Python\py\xunjian\\"+ip+".txt",'a') endtime = time.strftime('%Y-%m-%d %T') end_info = "巡检结束时间:"+str(endtime) log.write(start_info+'\n\n'+output+'\n\n'+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,Huawei@123192.168.10.12,admin,Huawei@123192.168.10.13,admin,Huawei@123192.168.10.14,admin,Huawei@123

以上就是python利用paramiko实现交换机巡检的示例的详细内容,更多关于paramiko交换机巡检的资料请关注其它相关文章!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章