标签:nec its mat issue lis soft err shc cep
1. python 版本,Microsoft Store 直接安装的:
D:\Documents\python lab\python\python_switch>python Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
2. paramiko 是一个实现SSH远程设备的模块,由于是第三方模块,需要先安装:
pip install paramiko
两个文件与脚本放在同一目录下
1. 新建名称为:ip_list.txt 的 txt 文件用来存放需要 SSH 的交换机IP地址
192.168.22.64 192.168.22.1
2. 新建名称为:cmd_list.txt 的 txt 文件用来存放需要配置的命令
system-view
undo FTP server
vlan 28
""" 作者:五个一斋 功能:批量SSH配置网络设备 版本:1.0 日期:2021年03月17日 """ # _*_ encoding:gbk _*_ # _*_ coding:UTF-8 _*_ import paramiko import time import getpass # 为了异常处理来应对网络设备不可达引起的socket.error,必须引入socket这个Python内建模块 import socket import io username = input(‘Username:‘) password = getpass.getpass(‘Password:‘) ip_file = ‘ip_list.txt‘ cmd_file = ‘cmd_list.txt‘ log = io.open(‘log.txt‘, ‘a‘, encoding=‘utf-8‘) # 创建两个空列表用于统计有哪些设备因为密码错误无法登录、设备本身问不可达无法登录 switch_with_authentication_issue = [] switch_not_reachable = [] iplist = open(ip_file, ‘r‘) for line in iplist.readlines(): try: ip = line.strip() ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=ip, username=username, password=password) print("恭喜你成功登录 ", ip) command = ssh_client.invoke_shell() cmdlist = open(cmd_file, ‘r‘) cmdlist.seek(0) for line in cmdlist.readlines(): command.send(line) time.sleep(1) cmdlist.close() output = command.recv(65535) print(bytes.decode(output)) bs = bytes.decode(output) log.write(bs) except paramiko.ssh_exception.AuthenticationException: print("密码错误 " + ip + ".") switch_with_authentication_issue.append(ip) except socket.error: print(ip + " 无法连接") switch_not_reachable.append(ip) iplist.close() ssh_client.close() print(‘\n因认证问题无法登录:‘) log.write("\n\n\n\n密码错误的交换机:\n") for i in switch_with_authentication_issue: print(i) log.write(i + "\n") print(‘\n无法连接以下交换机:‘) log.write("\n\n\n无法连接的交换机:\n") for i in switch_not_reachable: print(i) log.write(i + "\n") input("按回车关闭窗口!~")
D:\Documents\python lab\python3\python_switch>"C:/Program Files/Python38/python.exe" "d:/Documents/python lab/python3/python_switch/swconifg_v1.py" Username:admin Password: 恭喜你成功登录 192.168.22.64 Info: The max number of VTY users is 10, and the number of current VTY users on line is 2. The current login time is 2021-03-17 18:24:46+08:00. <switch>system-view Enter system view, return user view with Ctrl+Z. [switch]undo FTP server Info: Succeeded in closing the FTP server. [switch]vlan 28 192.168.22.1 无法连接 因认证问题无法登录: 无法连接以下交换机: 192.168.22.1 按回车关闭窗口!~
同时会在脚本文件目录下生成一个log.txt文件,记录命令运行的情况。
标签:nec its mat issue lis soft err shc cep
原文地址:https://www.cnblogs.com/zoujunjie/p/14551092.html