这期内容当中小编将会给大家带来有关python利用paramiko实现交换机巡检,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
#-*- coding:UTF-8 -*- import paramiko import time starttime = 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.txt display device display environment display alarm urgen display memory-usage display cpu-usage display logbuffer level 0 display logbuffer level 1 display logbuffer level 2 display logbuffer level 3 display logbuffer level 4 #设备信息 device_info.txt 192.168.10.11,admin,Huawei@123 192.168.10.12,admin,Huawei@123 192.168.10.13,admin,Huawei@123 192.168.10.14,admin,Huawei@123
上述就是小编为大家分享的python利用paramiko实现交换机巡检了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。