Python教程

4.巡检交换机-python3(后期加入多线程)

本文主要是介绍4.巡检交换机-python3(后期加入多线程),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

总结:

交换机的ssh得加入这个look_for_keys=False,不然提示

Authentication failed.

Authentication failed.

ssh.connect(hostname=ip,port=22,username='admin',password='admin123',look_for_keys=False)        #可填入aaa账户        

 此外,交换机巡检请打screen-length 0 temporary这个命令,让全页显示哈!!

1.ensp与windows loopback7(配置ip7.7.7.7)互联互通操作:参考https://blog.csdn.net/qq_42796807/article/details/103367120

 

image.png 

2.配置ssh

aaa

local-user admin password cipher admin123 privilege level 15

local-user admin service-type ssh  telnet


user-interface vty 0 4

authentication-mode aaa

protocol inbound ssh

 

rsa local-key-pair create


stelnet server enable

ssh user admin authentication-type password

 

 

 

 

3.配置ip

#

interface GigabitEthernet0/0/0

 ip address 7.7.7.1 255.255.255.0

#

 

 

 

4.写python代码

 

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

 

#备注1:

#功能:远程登录多台网络交换机执行相应命令输出到txt文本。其中服务器ip列表自定义,巡检命令自定义

 

#备注2:

#运行环境python3

 

#备注3:

#运行方式:

#C:\Users\Administrator\Desktop\d>python xunjian_python3.py ip_file.txt cmd_file.txt

#其中:

#ip_file.txt内容是需要巡检ip列表如:

#7.7.7.1

#7.7.7.1

#cmd_file.txt内容是自定义的巡检命令:

#screen-length 0 temporary

#display device

#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

 

#备注4:

#安装

#pip install paramiko

 

 

import os,sys

import paramiko

import time

import datetime

 

ip_file = sys.argv[1]

cmd_file = sys.argv[2]

 

 

def sshexeccmd(ip):

    try:

        ssh = paramiko.SSHClient()

        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        ssh.connect(hostname=ip,port=22,username='admin',password='admin123',look_for_keys=False)        #可填入aaa账户        

        print("You have successfully connect to ", ip)

       

        command = ssh.invoke_shell()

        cmdlist = open(cmd_file, 'r')

        cmdlist.seek(0)

        for line in cmdlist.readlines():

            command.send(line + "\n")

        time.sleep(3)

        cmdlist.close()

        output = command.recv(65535).decode('utf-8')

        #output = command.recv(65535)#如果用这排  会出现大量\r\n\r\n 而且整个文件在一排,得用.decode('utf-8') 解决

        t = datetime.datetime.now()

        #对现在时间格式化,以此作为文件名

        cur_time = t.strftime('%Y-%m-%d_%H-%M-%S')

 

        save_file = open(ip +  '_' + cur_time + '.txt',"w",encoding='utf-8')

        save_file.seek(0)

        save_file.write(str(output))

        save_file.close()

 

        ssh.close()

    except Exception as e:

        print(e)

 

def main():

    iplist = open(ip_file, 'r')

    for line in iplist.readlines():

        try:

            ip = line.strip()

            sshexeccmd(ip)

        except Exception as e:

            print(e)

 

 

if __name__ == '__main__':

    main() 

 

 

 

 

5.测试

 

C:\Users\Administrator\Desktop\20210421编写的自动化巡检python脚本\巡检交换机>python xunjian_python3.py ip_file.txt cmd_file.txt

 

C:\Users\Administrator\Desktop\20210421编写的自动化巡检python脚本\巡检交换机>python xunjian_python3.py ip_file.txt cmd_file.txt

You have successfully connect to  7.7.7.1

You have successfully connect to  7.7.7.1

 

C:\Users\Administrator\Desktop\20210421编写的自动化巡检python脚本\巡检交换机>

image.png


                                       


这篇关于4.巡检交换机-python3(后期加入多线程)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!