栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python自动化运维网络设备

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python自动化运维网络设备

sw1:

二层  vlan  Trunk  STP  Ether-trunk

sw1上

[sw1]vlan batch 100

[sw1] int g0/0/1

[sw1-GigabitEthernet0/0/1]port link-type trunk

[sw1-GigabitEthernet0/0/1]port trunk allow-pass vlan 100

[sw1-GigabitEthernet0/0/1]int g0/0/2

[sw1-GigabitEthernet0/0/2]port link-type trunk

[sw1-GigabitEthernet0/0/2] port trunk allow-pass vlan 100

[sw1-GigabitEthernet0/0/2]int g0/0/24

[sw1-GigabitEthernet0/0/24]port link-type trunk

[sw1-GigabitEthernet0/0/24]port trunk allow-pass vlan 100

[sw1]interface g 0/0/3

[sw1-GigabitEthernet0/0/3]port link-type  access

[sw1-GigabitEthernet0/0/3]port  default vlan 100

[sw1]stp enable

[sw1]stp root primary

sw2上

[sw2]vlan batch 100

[sw2]int g0/0/1

[sw2-GigabitEthernet0/0/1] port link-type trunk

[sw2-GigabitEthernet0/0/1] port trunk allow-pass vlan 100

[sw2-GigabitEthernet0/0/1]int g0/0/2

[sw2-GigabitEthernet0/0/2] port link-type trunk

[sw2-GigabitEthernet0/0/2] port trunk allow-pass vlan 100

[sw2-GigabitEthernet0/0/2]int g0/0/24

[sw2-GigabitEthernet0/0/24] port link-type trunk

[sw2-GigabitEthernet0/0/24] port trunk allow-pass vlan 100

sw3上

[sw3]vlan batch 100

[sw3]int g0/0/1

[sw3-GigabitEthernet0/0/1]port link-type trunk

[sw3-GigabitEthernet0/0/1] port trunk allow-pass vlan 100

[sw3-GigabitEthernet0/0/1]int g0/0/2

[sw3-GigabitEthernet0/0/2]port link-type trunk

[sw3-GigabitEthernet0/0/2] port trunk allow-pass vlan 100

Sw4上     

[sw4]vlan batch 100

[sw4]int g0/0/1

[sw4-GigabitEthernet0/0/1] port link-type trunk

[sw4-GigabitEthernet0/0/1] port trunk allow-pass vlan 100

[sw4-GigabitEthernet0/0/1]int g0/0/2

[sw4-GigabitEthernet0/0/2] port link-type trunk

[sw4-GigabitEthernet0/0/2] port trunk allow-pass vlan 100

三层  IP地址

[sw1]interface  vlanif  100

[sw1-Vlanif100]ip  address 192.168.100.1 24

[sw2]int vlan 100

[sw2-Vlanif100]ip address 192.168.100.2 24

[sw3]int vlan 100

[sw3-Vlanif100]ip address 192.168.100.3 24

[sw4]int vlan 100

[sw4-Vlanif100]ip address 192.168.100.4 24

交换机部署ssh  

sw1 

[sw1]dsa local-key-pair create

#认证形式  密码认证

[sw1]ssh user dyx authentication-type password

#登录类型

[sw1]ssh user dyx service-type stelnet

#开启ssh登录

[sw1]stelnet server enable

#开放vty 端口,能被远程登录

#开放编号0 1 2 3 4 的vty端口,同时支持五个远程用户连接

[sw1]user-interface vty 0 4

#认证模式为aaa---àauthentication 认证(谁能登录进来)

#              -----àauthorizathon授权 (谁进来能干什么)

#             -----à  according 审计 (谁进来干了什么)

[sw1-ui-vty0-4]authentication-mode aaa

#登录进来的协议是ssh

[sw1-ui-vty0-4] protocol inbound ssh

sw1上

[sw1]aaa

[sw1-aaa]local-user dyx password cipher Huawei@123

Info: Add a new user.

#授权

[sw1-aaa]local-user dyx privilege level 15

[sw1-aaa]local-user dyx service-type ssh

 

在sw2上

[sw2]ssh user dyx authentication-type  password

Info: Succeeded in adding a new SSH user.

[sw2]ssh user dyx service-type stelnet

[sw2]stelnet server enable

Info: Succeeded in starting the Stelnet server.

[sw2]user-interface vty 0 4

[sw2-ui-vty0-4]authentication-mode aaa

[sw2-ui-vty0-4]protocol inbound ssh

[sw2-ui-vty0-4]q

[sw2]aaa

[sw2-aaa]local-user dyx password cipher Huawei@123

Info: Add a new user.

[sw2-aaa]local-user dyx privilege level 15

[sw2-aaa]local-user dyx service-type ssh

在sw3上

[SW3]ssh user dyx authentication-type  password

Info: Succeeded in adding a new SSH user.

[SW3]ssh user dyx service-type stelnet

[SW3]stelnet server enable

Info: Succeeded in starting the Stelnet server.

[SW3]user-interface vty 0 4

[SW3-ui-vty0-4]authentication-mode aaa

[SW3-ui-vty0-4]protocol inbound ssh

[SW3-ui-vty0-4]q

[SW3]aaa

[SW3-aaa]local-user dyx password cipher Huawei@123

Info: Add a new user.

[SW3-aaa]local-user dyx privilege level 15

[SW3-aaa]local-user dyx service-type ssh

在sw4上

[SW4]ssh user dyx authentication-type  password

Info: Succeeded in adding a new SSH user.

[SW4]ssh user dyx service-type stelnet

[SW4]stelnet server enable

Info: Succeeded in starting the Stelnet server.

[SW4]user-interface vty 0 4

[SW4-ui-vty0-4]authentication-mode aaa

[SW4-ui-vty0-4]protocol inbound ssh

[SW4-ui-vty0-4]q

[SW4]aaa

[SW4-aaa]local-user dyx password cipher Huawei@123

Info: Add a new user.

[SW4-aaa]local-user dyx privilege level 15

[SW4-aaa]local-user dyx service-type ssh

实例一:

import paramiko
import time

# 1.创建SSH客户端对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 使用客户端 ,导入参数: hostname="192.168.100.1", username="dyx", password="Huawei@123"
ssh.connect(hostname="192.168.100.1", username="dyx", password="Huawei@123")

command = ssh.invoke_shell()  # 创建一个 命令对象,调用 shell

# 给设备发送配置命令(将发送的命令,字符串的形式转换成 utf-8 格式的二进制,传递给网络设备)
command.send("system-view  n".encode())
command.send("sys HSW1 n".encode())

time.sleep(1)  # 发送完命令,需要一个延时,等待设备 将回显值回传,等待时间不能太短
output = command.recv(65535)  # 接收服务器的返回数据
print(output.decode()) # 将接收的数据解码

ssh.close()  # 程序结束了,关闭连接

X-shell

实例二:

import paramiko
import time

# 1.创建SSH客户端对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 使用客户端 ,导入参数: hostname="192.168.100.2", username="dyx", password="Huawei@123"
ssh.connect(hostname="192.168.100.2", username="dyx", password="Huawei@123")

command = ssh.invoke_shell()  # 创建一个 命令对象,调用 shell

# 读取配置文件,将读取到的每一行,发送给 设备
with open('SW1.cfg', "r", encoding='utf-8') as f:
    while True:
        content = f.readline()
        if not content:
            break
        command.send(content.encode())

print("please wait a monment...")
time.sleep(5)  # 发送完命令,需要一个延时,等待设备 将回显值回传,等待时间不能太短
output = command.recv(65535)  # 接收服务器的返回数据
print(output.decode())  # 将接收的数据解码


ssh.close()  # 程序结束了,关闭连接

运行后

实例三:

import paramiko
import time
from device_info import *

# 需要一个数据库,存储着 各个设备的信息
for item in range(len(datebase)):  # range(len(datebase)---> 0-->3
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=datebase[item]["ip_add"], username=datebase[item]["username"],
                password=datebase[item]["passwd"])
    command = ssh.invoke_shell()
    with open(datebase[item]["path"], "r", encoding='utf-8') as f:
        while True:
            content = f.readline()
            if not content:
                break
            command.send(content.encode())

    print("please wait a monment...")
    time.sleep(5)
    output = command.recv(65535)
    print(output.decode())
    # 程序运行结束提示!
    done()

    ssh.close()

dict_SW1 = {"ip_add": "192.168.100.3", "username": "dyx", "passwd": "Huawei@123", "path": "SW1.cfg"}
dict_SW2 = {"ip_add": "192.168.100.4", "username": "dyx", "passwd": "Huawei@123", "path": "SW2.cfg"}

datebase = [dict_SW1, dict_SW2]


def done():
    print("""
                              
     8                    
     8                    
.oPYo8 .oPYo. odYo. .oPYo.
8    8 8    8 8' `8 8oooo8
8    8 8    8 8   8 8.    
`YooP' `YooP' 8   8 `Yooo'
:.....::.....:..::..:.....:
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
    """)

运行后

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号