使用paramiko在eNSP的交换机中批量创建VLAN

描述

来源:公众号【网络技术干货圈】

作者:圈圈

ID:wljsghq

实验拓扑:

python
cloud连接本机,ip地址为192.168.56.1,五台交换机的配置的地址为192.168.1.11~55。现在通过paramiko,ssh进入五台设备,并且在五台设备上分别创建vlan10-vlan20这11个VLAN。

版本:python3.9

实验步骤:

一、ssh配置:

 

## 创建秘钥
[sw2]dsa local-key-pair create

## 配置SSH认证类型(密码/其他)
[sw2]ssh user prin authentication-type password
[sw2]ssh user prin service-type stelnet
[sw2]stelnet server enable

## 配置认证模式
[sw2]user-interface vty 0 4
[sw2-ui-vty0-4]authentication-mode aaa  //配置认证模式
[sw2-ui-vty0-4]protocol inbound ssh     //允许 ssh 连接虚拟终端

## 配置本地用户信息
[sw2]aaa
[sw2-aaa] local-user prin password cipher Huawei@123
[sw2-aaa]local-user prin privilege level 15
[sw2-aaa] local-user prin service-type ssh

 

二、python脚本:

 

import paramiko
import time
import getpass

#使用input函数,输入SSH的用户名
username = input('Username: ')
#通过getpass()函数接收密码,密码是不可见的,但是在windows上有bug,密码可见
password = getpass.getpass('Password: ')

#创建一个列表,表示五台设备最后8位的地址
ip_tail_list = [11, 22, 33, 44, 55]

#使用for循环,接受SSH的秘钥,并分别依次连接到五台设备,注意需要将i转化为字符串
for i in ip_tail_list:
    ip = "192.168.56." + str(i) 
    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)

#使用invoke_shell()唤醒shell界面
    command = ssh_client.invoke_shell()

#使用command.send()函数创建VLAN,并且设置每个VLAN的描述;未来保证设备能够正常接受配置,每次创建1个VLAN后休息1s
    command.send("system 
")

    for n in range(10, 21):
        print("Creating Vlan " + str(n))
        command.send("vlan " + str(n) + "
")
        command.send("description Python Vlan" + str(n) + "
")
        time.sleep(1)

#保存配置,并且通过command.recv()函数得到回信的信息,最多接受65535个字符
    command.send("return 
")
    command.send("save 
"+"y 
"+"
")
    time.sleep(2)
    output = command.recv(65535)
    print(output.decode('ascii'))

#关闭连接
ssh_client.close()

 

如果管理的设备数目过多,可以直接通过读取txt文件的方式获取IP地址,仅需要将如下代码:

 

#创建一个列表,表示五台设备最后8位的地址
ip_tail_list = [11, 22, 33, 44, 55]

#使用for循环,接受SSH的秘钥,并分别依次连接到五台设备,注意需要将i转化为字符串
for i in ip_tail_list:
    ip = "192.168.56." + str(i) 
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password)
#......省略中间部分
ssh_client.close()

 

更换为下述即可:

 

#使用open()函数打开ip_list文件,并将读取的结果赋予f
f = open("ip_list.txt","r")

#调用readlines()函数,返回IP地址的列表,并使用for循环遍历;注意使用readlines()的每一个ip地址后带有
,需要通过strip()函数去除
for i in f.readlines():
    ip = i.strip()
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password)
#.......省略中间部分,在完成文件操作后,关闭文件
f.close()
ssh_client.close()

 

执行效果:

python

在设备上检查是否配置成功,以SW1为例:
python
可以看到创建VLAN和添加VLAN描述成功。

  审核编辑:汤梓红
 
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分