前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用Python脚本批量登录交换机?

如何使用Python脚本批量登录交换机?

作者头像
网络技术联盟站
发布2023-03-01 21:35:43
6320
发布2023-03-01 21:35:43
举报
前言:

Environment: python = 3.9.0 netmiko = 4.1.0 (pip install netmiko) pyyaml = 6.0 (pip install pyyaml)

文件内容

为方便以后管理,使用YAML文件作为设备库(也可以使用json,但YAML更直观:)。 inventory.yml 如下:

代码语言:javascript
复制
# inventory.yml
SW01:
  device_type: cisco_ios
  host: 10.0.0.1
  username: wljslmz1
  password: wljslmz123
  port: 22

SW02:
  device_type: cisco_ios
  host: 10.0.0.2
  username: wljslmz2
  password: wljslmz456
  port: 22

SW03:
  device_type: huawei
  host: 10.0.0.3
  username: wljslmz3
  password: wljslmz789
  port: 22
  conn_timeout: 15 # 华为S5720交换机假如连接超时的话加上

main.py 如下:

代码语言:javascript
复制
# main.py
import yaml
from netmiko import ConnectHandler
from netmiko.exceptions import NetmikoTimeoutException, NetmikoAuthenticationException

with open('inventory.yml', 'r') as f:
    devs = yaml.safe_load(f)

for hostname, conn_info in devs.items():
    conn_info.setdefault('session_log', f'{hostname}_logs.txt')
    try:
        with ConnectHandler(**conn_info) as conn:
            print(f'[{hostname}] connected -> {conn.find_prompt()}')
    except NetmikoTimeoutException:
        print(f'ERROR: [{hostname}] connect timeout!')
    except NetmikoAuthenticationException:
        print(f'ERROR: [{hostname}] please check your username & password!')

输出效果

代码语言:javascript
复制
$ python main.py
ERROR: [SW01] connect timeout!
ERROR: [SW02] please check your username & password!
[SW03] connected -> <Huawei>

代码解释:

1. 从设备库中读取设备参数为字典格式:

代码语言:javascript
复制
import yaml
with open('inventory.yml', 'r') as f:
    devs = yaml.safe_load(f)
代码语言:javascript
复制
# print(devs)
{
  'SW01': {
    'device_type': 'cisco_ios', 
    'host': '10.0.0.1', 
    'username': 'wljslmz1', 
    'password': 'wljslmz123', 
    'port': 22
  }, 
  'SW02': {
    'device_type': 'cisco_ios', 
    'host': '10.0.0.2', 
    'username': 'wljslmz2', 
    'password': 'wljslmz456', 
    'port': 22
  }, 
  'SW03': {
    'device_type': 'huawei', 
    'host': '10.0.0.3', 
    'username': 'wljslmz3', 
    'password': 'wljslmz789', 
    'port': 22, 
    'conn_timeout': 15
  }
}

2. 利用读取的设备字典,通过netmiko连接,捕捉连接超时和验证错误,同时记录日志。

代码语言:javascript
复制
from netmiko import ConnectHandler
from netmiko.exceptions import NetmikoTimeoutException, NetmikoAuthenticationException
for hostname, conn_info in devs.items():
    # 添加参数,将登录过程记录到同目录下的“{设备名}_logs.txt” (也可以在inventory.yml里设置)
    conn_info.setdefault('session_log', f'{hostname}_logs.txt')
    try:
        with ConnectHandler(**conn_info) as conn:
            print(f'[{hostname}] connected -> {conn.find_prompt()}')
    except NetmikoTimeoutException: # 连接超时
        print(f'ERROR: [{hostname}] connect timeout!')
    except NetmikoAuthenticationException: # 验证失败
        print(f'ERROR: [{hostname}] please check your username & password!')
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 网络技术联盟站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文件内容
  • 输出效果
  • 代码解释:
    • 1. 从设备库中读取设备参数为字典格式:
      • 2. 利用读取的设备字典,通过netmiko连接,捕捉连接超时和验证错误,同时记录日志。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档