如何使用 Python 和 Netmiko 自动创建网络设备 VLAN,受益颇深!

简介: 【5月更文挑战第9天】

你好,这里是网络技术联盟站,我是瑞哥。

在现代网络中,自动化是网络管理的重要一环。通过使用Python编程语言和Netmiko库,我们可以简化并提高VLAN(Virtual Local Area Network)的创建过程。VLAN允许我们将网络设备划分为逻辑上的独立网络,提高网络的性能、安全性和管理效率。

在开始自动执行VLAN创建之前,我们需要进行一些准备工作,确保系统处于适当的状态。

安装 Python

在Windows上安装Python

  1. 访问Python官方网站

前往 Python官方网站https://www.python.org/downloads下载最新版本的Python。

  1. 下载安装程序

在网站上选择适用于Windows的Python版本,通常为一个exe文件。双击下载的安装程序以启动安装。

  1. 设置安装选项

在安装过程中,确保勾选 "Add Python to PATH" 选项,这将使Python可在命令行中直接访问。

  1. 完成安装

点击 "Install Now" 开始安装,等待安装程序完成。

  1. 验证安装

打开命令提示符(Command Prompt)或PowerShell,并输入以下命令来验证Python是否正确安装:

python --version

或者,使用以下命令查看Python的交互式Shell:

python

如果成功安装,你将看到Python版本信息。

在Mac上安装Python

Mac通常预装有Python,但为了确保使用最新版本或进行自定义安装,可以按照以下步骤操作:

  1. 使用Homebrew安装(推荐):

如果你使用了Homebrewhttps://brew.sh包管理器,可以使用以下命令安装Python:

brew install python
  1. 手动安装

前往Python官方网站https://www.python.org/downloads下载最新版本的Python。

  1. 安装

双击下载的pkg文件,按照提示完成安装。

  1. 验证安装

打开终端,并输入以下命令验证Python是否正确安装:

python3 --version

或者,使用以下命令查看Python的交互式Shell:

python3

如果成功安装,你将看到Python版本信息。

在Linux上安装Python

大多数Linux发行版都预装有Python,你可以通过包管理器安装或更新。

使用包管理器

  1. Debian/Ubuntu
sudo apt-get update
sudo apt-get install python3
  1. Fedora
sudo dnf install python3
  1. openSUSE
sudo zypper install python3

通过源代码安装

  1. 下载源代码

前往Python官方网站https://www.python.org/downloads/source下载Python的源代码压缩包。

  1. 解压缩源代码

在终端中进入解压缩后的目录,并执行以下命令:

./configure
make
sudo make install
  1. 验证安装

在终端中输入以下命令验证Python是否正确安装:

python3 --version

或者,使用以下命令查看Python的交互式Shell:

python3

如果成功安装,你将看到Python版本信息。

设置虚拟环境

虚拟环境可以隔离项目的依赖,防止与其他项目冲突。使用以下命令创建和激活虚拟环境:

# 安装 virtualenv
pip install virtualenv

# 创建虚拟环境
python -m venv myenv

# 激活虚拟环境
# 在 Windows 上:
myenv\Scripts\activate
# 在 Linux 或 macOS 上:
source myenv/bin/activate

安装 Netmiko

Netmiko是一个用于处理网络设备的Python库,支持多种设备类型。使用以下命令安装Netmiko:

pip install netmiko

Netmiko基于SSH协议,因此确保你的网络设备支持SSH,并且已经正确配置和启用。

编写 Python 脚本

我们将创建一个简单的Python脚本,该脚本连接到Cisco设备并执行VLAN创建过程。

连接到设备

首先,我们需要建立与网络设备的连接。创建一个Python脚本,命名为 create_vlan.py

from netmiko import ConnectHandler

device = {
   
   
    'device_type': 'cisco_ios',
    'ip': '设备IP地址',
    'username': '你的用户名',
    'password': '你的密码',
}

connection = ConnectHandler(**device)

这段代码使用Netmiko的 ConnectHandler 类建立到Cisco设备的SSH连接。请替换 '设备IP地址''你的用户名''你的密码' 为实际设备的信息。

创建 VLAN

接下来,我们将编写代码来创建一个新的VLAN。在Cisco设备上,我们可以使用 vlan 命令来完成。

vlan_id = 100
vlan_name = 'vlan_name'

vlan_command = f"vlan {vlan_id}\nname {vlan_name}"
connection.send_config_set([vlan_command])

这段代码定义了VLAN的ID和名称,然后使用 send_config_set 方法将配置命令发送到设备。这里我们使用了格式化字符串 f"vlan {vlan_id}\nname {vlan_name}" 来生成VLAN配置命令。

验证配置

为了确保VLAN创建成功,我们可以执行 show vlan brief 命令,并输出结果。

output = connection.send_command('show vlan brief')
print(output)

这段代码使用 send_command 方法向设备发送命令,并将结果存储在 output 变量中。然后通过 print(output) 打印结果。

关闭连接

最后,我们在脚本的末尾关闭与设备的连接,释放资源。

connection.disconnect()

完整的脚本如下:

from netmiko import ConnectHandler

device = {
   
   
    'device_type': 'cisco_ios',
    'ip': '设备IP地址',
    'username': '你的用户名',
    'password': '你的密码',
}

connection = ConnectHandler(**device)

# 创建 VLAN
vlan_id = 100
vlan_name = 'vlan_name'
vlan_command = f"vlan {vlan_id}\nname {vlan_name}"
connection.send_config_set([vlan_command])

# 验证配置
output = connection.send_command('show vlan brief')
print(output)

# 关闭连接
connection.disconnect()

保存并运行脚本:

python create_vlan.py

添加错误处理

在真实的网络环境中,很可能会遇到各种问题,如设备不可达、认证失败等。为了增加脚本的鲁棒性,我们可以添加一些错误处理。

from netmiko import ConnectHandler, SSHException

device = {
   
   
    'device_type': 'cisco_ios',
    'ip': '设备IP地址',
    'username': '你的用户名',
    'password': '你的密码',
}

try:
    connection = ConnectHandler(**device)

    # 创建 VLAN
    vlan_id = 100
    vlan_name = 'vlan_name'
    vlan_command = f"vlan {vlan_id}\nname {vlan_name}"
    connection.send_config_set([vlan_command])

    # 验证配置
    output = connection.send_command('show vlan brief')
    print(output)

except SSHException as e:
    print(f"SSH连接错误: {e}")

finally:
    # 关闭连接
    if 'connection' in locals():
        connection.disconnect()

在这个脚本中,我们使用了 tryexceptfinally 来进行错误处理。如果在 try 块中发生SSHException,脚本将捕获异常并打印错误消息,然后执行 finally 块中的关闭连接操作。

从配置文件读取设备信息

为了增加脚本的灵活性,我们可以将设备信息存储在一个单独的配置文件中,以便在不同场景下轻松修改。

创建一个名为 device_config.ini 的配置文件:

[device]
device_type = cisco_ios
ip = 设备IP地址
username = 你的用户名
password = 你的密码

修改脚本以从配置文件读取设备信息:

from netmiko import ConnectHandler, SSHException
from configparser import ConfigParser

config = ConfigParser()
config.read('device_config.ini')

device = {
   
   
    'device_type': config['device']['device_type'],
    'ip': config['device']['ip'],
    'username': config['device']['username'],
    'password': config['device']['password'],
}

try:
    connection = ConnectHandler(**device)

    # 创建 VLAN
    vlan_id = 100
    vlan_name = 'vlan_name'
    vlan_command = f"vlan {vlan_id}\nname {vlan_name}"
    connection.send_config_set([vlan_command])

    # 验证配置
    output = connection.send_command('show vlan brief')
    print(output)

except SSHException as e:
    print(f"SSH连接错误: {e}")

finally:
    # 关闭连接
    if 'connection' in locals():
        connection.disconnect()

这样,你可以通过修改 device_config.ini 文件来轻松更改设备信息,而无需直接修改脚本。

批量创建 VLAN

在实际网络中,可能需要一次性在多个设备上创建相同的VLAN。为了实现这一点,我们可以将设备信息存储在一个列表中,并使用循环在每个设备上执行相同的操作。

from netmiko import ConnectHandler, SSHException
from configparser import ConfigParser

config = ConfigParser()
config.read('device_config.ini')

devices = [
    {
   
   
        'device_type': config['device']['device_type'],
        'ip': '设备1的IP地址',
        'username': config['device']['username'],
        'password': config['device']['password'],
    },
    {
   
   
        'device_type': config['device']['device_type'],
        'ip': '设备2的IP地址',
        'username': config['device']['username'],
        'password': config['device']['password'],
    },
    # 添加更多设备
]

for device in devices:
    try:
        connection = ConnectHandler(**device)

        # 创建 VLAN
        vlan_id = 100
        vlan_name = 'vlan_name'
        vlan_command = f"vlan {vlan_id}\nname {vlan_name}"
        connection.send_config_set([vlan_command])

        # 验证配置
        output = connection.send_command('show vlan brief')
        print(output)

    except SSHException as e:
        print(f"SSH连接错误({device['ip']}): {e}")

    finally:
        # 关闭连接
        if 'connection' in locals():
            connection.disconnect()

通过在 devices 列表中添加更多设备,可以在一次运行中批量创建相同的VLAN。

使用函数进行模块化

为了提高脚本的可读性和维护性,我们可以将不同的功能模块化,将其放入函数中。

from netmiko import ConnectHandler, SSHException
from configparser import ConfigParser

def connect_to_device(device):
    try:
        connection = ConnectHandler(**device)
        return connection
    except SSHException as e:
        print(f"SSH连接错误({device['ip']}): {e}")
        return None

def create_vlan(connection, vlan_id, vlan_name):
    vlan_command = f"vlan {vlan_id}\nname {vlan_name}"
    connection.send_config_set([vlan_command])

def verify_vlan(connection):
    output = connection.send_command('show vlan brief')
    print(output)

def disconnect_from_device(connection):
    connection.disconnect()

def main():
    config = ConfigParser()
    config.read('device_config.ini')

    devices = [
        {
   
   
            'device_type': config['device']['device_type'],
            'ip': '设备1的IP地址',
            'username': config['device']['username'],
            'password': config['device']['password'],
        },
        {
   
   
            'device_type': config['device']['device_type'],
            'ip': '设备2的IP地址',
            'username': config['device']['username'],
            'password': config['device']['password'],
        },
        # 添加更多设备
    ]

    for device in devices:
        connection = connect_to_device(device)
        if connection:
            create_vlan(connection, vlan_id=100, vlan_name='vlan_name')
            verify_vlan(connection)
            disconnect_from_device(connection)

if __name__ == "__main__":
    main()

通过将连接设备、创建VLAN、验证配置和断开连接等操作封装成函数,我们可以更清晰地组织脚本,并使其更易于维护。

使用 Jinja2 模板生成配置

在实际网络环境中,可能会有大量相似的配置任务,例如在多个设备上创建相同的VLAN。为了更灵活地生成配置,我们可以使用 Jinja2 模板引擎。

首先,安装 Jinja2:

pip install jinja2

然后,创建一个 Jinja2 模板文件,命名为 vlan_template.j2

vlan {
  
  { vlan_id }}
 name {
  
  { vlan_name }}

修改脚本,使用 Jinja2 模板引擎加载模板并生成配置:

from netmiko import ConnectHandler, SSHException
from configparser import ConfigParser
from jinja2 import Environment, FileSystemLoader

def render_template(template_path, **kwargs):
    env = Environment(loader=FileSystemLoader('.'))
    template = env.get_template(template_path)
    return template.render(kwargs)

def connect_to_device(device):
    try:
        connection = ConnectHandler(**device)
        return connection
    except SSHException as e:
        print(f"SSH连接错误({device['ip']}): {e}")
        return None

def create_vlan(connection, vlan_id, vlan_name):
    vlan_template = render_template('vlan_template.j2', vlan_id=vlan_id, vlan_name=vlan_name)
    connection.send_config_set([vlan_template])

def verify_vlan(connection):
    output = connection.send_command('show vlan brief')
    print(output)

def disconnect_from_device(connection):
    connection.disconnect()

def main():
    config = ConfigParser()
    config.read('device_config.ini')

    devices = [
        {
   
   
            'device_type': config['device']['device_type'],
            'ip': '设备1的IP地址',
            'username': config['device']['username'],
            'password': config['device']['password'],
        },
        {
   
   
            'device_type': config['device']['device_type'],
            'ip': '设备2的IP地址',
            'username': config['device']['username'],
            'password': config['device']['password'],
        },
        # 添加更多设备
    ]

    for device in devices:
        connection = connect_to_device(device)
        if connection:
            create_vlan(connection, vlan_id=100, vlan_name='vlan_name')
            verify_vlan(connection)
            disconnect_from_device(connection)

if __name__ == "__main__":
    main()

这样,我们可以使用 Jinja2 模板生成更复杂的配置,并轻松地在不同的设备上执行相同的配置任务。

使用异常处理改进脚本稳定性

在网络自动化中,异常处理是至关重要的。当我们尝试连接设备、发送配置或执行其他操作时,可能会遇到各种错误。通过适当的异常处理,我们可以使脚本更加稳定和可靠。

from netmiko import ConnectHandler, SSHException
from configparser import ConfigParser

def connect_to_device(device):
    try:
        connection = ConnectHandler(**device)
        return connection
    except SSHException as e:
        print(f"SSH连接错误({device['ip']}): {e}")
        return None
    except Exception as e:
        print(f"连接设备({device['ip']})时出现错误: {e}")
        return None

def create_vlan(connection, vlan_id, vlan_name):
    try:
        vlan_command = f"vlan {vlan_id}\nname {vlan_name}"
        connection.send_config_set([vlan_command])
    except Exception as e:
        print(f"创建VLAN({vlan_id})时出现错误: {e}")

def verify_vlan(connection):
    try:
        output = connection.send_command('show vlan brief')
        print(output)
    except Exception as e:
        print(f"验证VLAN时出现错误: {e}")

def disconnect_from_device(connection):
    try:
        connection.disconnect()
    except Exception as e:
        print(f"断开连接时出现错误: {e}")

def main():
    config = ConfigParser()
    config.read('device_config.ini')

    devices = [
        {
   
   
            'device_type': config['device']['device_type'],
            'ip': '设备1的IP地址',
            'username': config['device']['username'],
            'password': config['device']['password'],
        },
        {
   
   
            'device_type': config['device']['device_type'],
            'ip': '设备2的IP地址',
            'username': config['device']['username'],
            'password': config['device']['password'],
        },
        # 添加更多设备
    ]

    for device in devices:
        connection = connect_to_device(device)
        if connection:
            create_vlan(connection, vlan_id=100, vlan_name='vlan_name')
            verify_vlan(connection)
            disconnect_from_device(connection)

if __name__ == "__main__":
    main()

通过在关键部分添加适当的异常处理,我们可以提高脚本的鲁棒性,并更好地处理可能出现的问题。

使用日志记录

在复杂的自动化脚本中,输出到控制台的信息可能不足以进行详细的故障排查。使用日志记录可以帮助我们跟踪脚本的执行过程,记录关键事件和错误。

import logging

logging.basicConfig(filename='automation.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def connect_to_device(device):
    try:
        connection = ConnectHandler(**device)
        logging.info(f"连接到设备({device['ip']})成功")
        return connection
    except SSHException as e:
        logging.error(f"SSH连接错误({device['ip']}): {e}")
        return None
    except Exception as e:
        logging.error(f"连接设备({device['ip']})时出现错误: {e}")
        return None

# 在其他关键部分添加日志记录

通过配置日志记录,我们可以在执行过程中捕获关键事件,并在需要时检查日志文件以了解脚本的行为。

使用版本控制

版本控制是软件开发中的重要实践之一,它允许我们跟踪代码的变化并协作开发。将脚本存储在版本控制系统(如Git)中可以使我们更好地管理代码,并跟踪变更历史。

通过将脚本存储在Git仓库中,并定期提交和推送变更,我们可以确保脚本的安全和可追溯性。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
5天前
|
存储 安全 网络安全
网络设备的部署(串行与并行)
网络设备的部署(串行与并行)
14 4
|
5天前
|
存储 前端开发 机器人
Python网络数据抓取(6):Scrapy 实战
Python网络数据抓取(6):Scrapy 实战
23 2
|
5天前
|
机器学习/深度学习 Python
【Python实战】——神经网络识别手写数字(三)
【Python实战】——神经网络识别手写数字
|
5天前
|
机器学习/深度学习 数据可视化 Python
【Python实战】——神经网络识别手写数字(二)
【Python实战】——神经网络识别手写数字(三)
|
2天前
|
网络协议 Python
Python 网络编程实战:构建高效的网络应用
【5月更文挑战第18天】Python在数字化时代成为构建网络应用的热门语言,因其简洁的语法和强大功能。本文介绍了网络编程基础知识,包括TCP和UDP套接字,强调异步编程、数据压缩和连接池的关键作用。提供了一个简单的TCP服务器和客户端代码示例,并提及优化与改进方向,鼓励读者通过实践提升网络应用性能。
19 6
|
2天前
|
网络协议 网络架构 Python
Python 网络编程基础:套接字(Sockets)入门与实践
【5月更文挑战第18天】Python网络编程中的套接字是程序间通信的基础,分为TCP和UDP。TCP套接字涉及创建服务器套接字、绑定地址和端口、监听、接受连接及数据交换。UDP套接字则无连接状态。示例展示了TCP服务器和客户端如何使用套接字通信。注意选择唯一地址和端口,处理异常以确保健壮性。学习套接字可为构建网络应用打下基础。
18 7
|
2天前
|
机器学习/深度学习 数据采集 自然语言处理
叮!你需要的Python面试指南以送到!,计算机网络面试知识
叮!你需要的Python面试指南以送到!,计算机网络面试知识
|
4天前
|
机器学习/深度学习 数据可视化 PyTorch
使用Python实现深度学习模型:生成对抗网络(GAN)
使用Python实现深度学习模型:生成对抗网络(GAN)
19 3
|
4天前
|
网络协议 Unix Python
Python编程-----网络通信
Python编程-----网络通信
8 1
|
5天前
|
缓存 负载均衡 应用服务中间件
python怎么发起网络请求?
python怎么发起网络请求?
15 0