Python实现网络自动化eNSP

 更新时间:2021年05月27日 09:33:23   作者:text1.txt  
这篇文章主要介绍了Python实现网络自动化eNSP,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.使用Paramiko登陆到单台交换机

实验拓扑

云彩桥接到本机环回接口:192.168.1.1/24
三层交换机IP:192.168.1.2/24

在这里插入图片描述

实验要求

使用Python Paramiko 模块实现SSH 登录单个交换机(192.168.56.2/24),配置LoopBack0地址:1.1.1.1/32。配置完成后,保存退出。

实验步骤 配置交换机管理地址,并测试与主机虚拟网卡连通性

[Huawei]vlan 10
[Huawei]int vlan 10
[Huawei-Vlanif10]ip add 192.168.1.2 24
[Huawei-GigabitEthernet0/0/1]port link-type access 
[Huawei-GigabitEthernet0/0/1]port default vlan 10

在这里插入图片描述

在这里插入图片描述

配置三层交换机开启 SSH 服务端,配置 SSH 账号密码。

[Huawei]user-interface vty 0 4
[Huawei-ui-vty0-4]authentication-mode aaa
[Huawei-ui-vty0-4]protocol inbound ssh
[Huawei-aaa]local-user python password cipher 123
[Huawei-aaa]local-user python privilege level 3
[Huawei-aaa]local-user python service-type ssh 
[Huawei]stelnet server enable 
[Huawei]ssh authentication-type default password 

Python代码

import paramiko
import time

ip = '192.168.56.2'
username = 'python'
password = '123'

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) //默认情况下,Paramiko会拒绝任何未知的SSH public keys,使用此函数使其接收来自交换机提供的public keys。
ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
print('Successfully connect to ' + ip)

commend = ssh_client.invoke_shell()
commend.send('sys\n')
commend.send('interface LoopBack 0\n')
commend.send('ip add 1.1.1.1 255.255.255.255\n')
commend.send('return\n')
commend.send('save\n')
commend.send('y\n')

time.sleep(3) //稍等3秒,然后执行以下操作
output = commend.recv(65535) //截取本次运行script后的所有输出记录,将其assign给output变量
print(output.decode("ascii"))

ssh_client.close()

查看运行结果

在这里插入图片描述

在交换机上查看

在这里插入图片描述

也可以在交换机上debuggiing ip packet可以看到日志

2.使用Paramiko登陆到连续子网交换机

实验拓扑

连续子网三层交换机:管理地址 192.168.1.2/24 to 192.168.1.5/24

在这里插入图片描述

实验要求

登陆到各台交换机,并为其配置vlan 11 to 15,保存配置并退出。

实验步骤

配置管理口IP地址,并配置SSH Server 登陆名以及密码等

python代码

import paramiko
import time

#import getpass
#username = input('Username: ')
#password = getpass.getpass('Password: ') //pycharm中该模块运行没反应,用户名和密码还是直接写的

username = 'python'
password = '123'

for i in range(2, 6):
    ip = '192.168.1.' + str(i)
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
    command = ssh_client.invoke_shell()
    print('Successfully connect to ' + ip)
    command.send('sys\n')
    for j in range(11, 16):
        print('正在创建VLAN: ' + str(j))
        command.send('vlan ' + str(j) + '\n')
        time.sleep(1)
    command.send('return\n')
    command.send('save\n')
    command.send('y\n')
    time.sleep(2)
    output = command.recv(65535).decode('ascii')
    print(output)

ssh_client.close()

运行结果

在这里插入图片描述

在这里插入图片描述

3.Paramiko登陆不连续子网交换机

实验拓扑

将交换机LSW5的管理接口ip更改为192.168.1.6/24,使交换机ip不在同一网段

在这里插入图片描述

实验要求

使用Paramiko登陆四台ip不连续的交换机,并给其配置vlan11 to 15

实验步骤

创建一个文本文档,将需要配置的交换机的ip地址写入,这里我在Desktop下创建了一个名为ip.txt文档

在这里插入图片描述

使用open函数,打开文件,进行操作,实现不连续子网调用

import paramiko
import time

username = 'python'
password = '123'

f = open('C:/Users/DELL/Desktop/ip.txt', 'r')
for line in f.readlines():
    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('Successfully connect to ', ip)
    command = ssh_client.invoke_shell()
    command.send('sys\n')
    command.send('vlan batch 11 to 15\n')
    time.sleep(2)
    command.send('return\n')
    command.send('save\n')
    command.send('y\n')
    time.sleep(2)
    output = command.recv(65535).decode('ascii')
    print(output)

f.close()
ssh_client.close()

查看运行结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4.sys.argv[ ] 实现灵活调用脚本所需文件

实验拓扑

假设1.2和1.3为一组,1.4和1.6为一组

在这里插入图片描述

实验要求

同时修改不同型号设备的配置,给SW1/3配置vlan11 to 15,SW4/5配置vlan16 to 20

实验步骤

创建两个名为ip1.txt,command1.txt的文件,存储1组的ip和要进行的配置

在这里插入图片描述

同样创建两个名为ip2.txt,command2.txt文件,存储2组的ip和要进行的配置

在这里插入图片描述

在这里插入图片描述

python代码

import paramiko
import time
import sys

username = 'python'
password = '123'

ip_file = sys.argv[1]
cmd_file = sys.argv[2]

iplist = open(ip_file)
for line in iplist.readlines():
    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('Successfully connect to ', ip)
    command = ssh_client.invoke_shell()
    cmdlist = open(cmd_file, 'r')
    cmdlist.seek(0)
    for line in cmdlist.readlines():
        command.send(line + '\n')
        time.sleep(5)
    cmdlist.close()
    output = command.recv(65535)
    print(output)

iplist.close()
ssh_client.close()

查看运行结果(pycharm不可以使用argv,在cmd里使用)

在这里插入图片描述

5.SSH连接失败处理

import paramiko
import time
import sys
import socket
import getpass

username = input('Username: ')
password = getpass.getpass('Password: ')
ip_file = sys.argv[1]
cmd_file = sys.argv[2]

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,look_for_keys=False)
        print('Successfully connect to ' + ip)
        command = ssh_client.invoke_shell()
        cmdlist = open(cmd_file, 'r')
        cmdlist.seek(0)
        for cmd in cmdlist.readlines():
            command.send(cmd + '\n')
        time.sleep(1)
        cmdlist.close()
        output = command.recv(65535)
        print(output.decode("ascii"))
    except paramiko.ssh_exception.AuthenticationException:
        print('User authentication failed for ' + ip + '.')
        switch_with_authentication_issue.append(ip)
    except TimeoutError:
        switch_not_reachable.append(ip)

iplist.close()
ssh_client.close()

print('\nUser authentication failed for below switches: ')
for i in switch_with_authentication_issue:
    print(i)

print('\nBelow switchs are not reachable: ')
for i in  switch_not_reachable:
    print(i)

到此这篇关于Python实现网络自动化eNSP的实现的文章就介绍到这了,更多相关Python 网络自动化 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python实现确认字符串是否包含指定字符串的实例

    Python实现确认字符串是否包含指定字符串的实例

    下面小编就为大家分享一篇Python实现确认字符串是否包含指定字符串的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • Python HTTP下载文件并显示下载进度条功能的实现

    Python HTTP下载文件并显示下载进度条功能的实现

    这篇文章主要介绍了Python HTTP下载文件并显示下载进度条功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Python程序打包工具py2exe和PyInstaller详解

    Python程序打包工具py2exe和PyInstaller详解

    这篇文章主要介绍了Python程序打包工具py2exe和PyInstaller详解,如果可以提前将程序打包成 Windows平台的 .exe 文件或者是Linux下的 .sh 脚本,那么使用起来就会方便很多,需要的朋友可以参考下
    2019-06-06
  • Python实现的读取电脑硬件信息功能示例

    Python实现的读取电脑硬件信息功能示例

    这篇文章主要介绍了Python实现的读取电脑硬件信息功能,结合实例形式分析了Python基于wmi库读取电脑CPU、磁盘、网络、进程等硬件信息相关操作技巧,需要的朋友可以参考下
    2018-05-05
  • Python+Pygame实现趣味足球游戏

    Python+Pygame实现趣味足球游戏

    这篇文章主要为大家分享了一个基于Python和Pygame实现的一个趣味足球游戏,文中的示例代码讲解详细,对我们学习Python有一定帮助,需要的可以参考一下
    2022-05-05
  • 微信公众号token验证失败解决方案

    微信公众号token验证失败解决方案

    这篇文章主要介绍了微信公众号token验证失败解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • python 使用while写猜年龄小游戏过程解析

    python 使用while写猜年龄小游戏过程解析

    这篇文章主要介绍了python 使用while写猜年龄小游戏过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • python中随机函数random用法实例

    python中随机函数random用法实例

    这篇文章主要介绍了python中随机函数random用法,实例分析了random函数的相关使用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • python DataFrame 修改列的顺序实例

    python DataFrame 修改列的顺序实例

    下面小编就为大家分享一篇python DataFrame 修改列的顺序实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 详细介绍Scrapy shell的使用教程

    详细介绍Scrapy shell的使用教程

    Scrapy shell是一个非常有用的工具,可以帮助开发者快速地测试和调试Scrapy的爬虫代码,这篇文章主要介绍了详细介绍Scrapy shell的使用,需要的朋友可以参考下
    2023-05-05

最新评论