实验2 循环修改交换机VLAN

配合 getpass 模块和 input() 函数实现交互式的 SSH ⽤户名和密码输⼊。
配合 for loop 同时给 5 台交换机配置 VLAN 201 ⾄ 205 。

# import 导包
import getpass
import paramiko
import time


# 定义两个字符串变量
username = input("Username: ")
print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
password = getpass.getpass("Password: ")
print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
for i in range(201, 206):
    ip = '192.168.8.' + str(i)
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password, allow_agent=False, look_for_keys=False)
    command = ssh_client.invoke_shell()
    print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
    print('已经成功登陆交换机 Layer3Switch-' + str(i - 200) + ' ' + ip)
    # 关闭分屏功能
    command.send('screen-length 0 temporary\n')
    # 进⼊系统视图
    command.send('sys\n')
    for j in range(201, 206):
        print('正在创建 VLAN :' + str(j))
        command.send('vlan ' + str(j) + '\n')
        time.sleep(1)
        command.send('desc Python_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()

C:\Users\82127\.conda\envs\pythonProject\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.1\plugins\python-ce\helpers\pydev\pydevd.py" --multiprocess --qt-support=auto --client 127.0.0.1 --port 51862 --file E:/log/python/pythonProject/Demo02.py
已连接到 pydev 调试器(内部版本号 221.5591.52)Username: =-=-=-=-=-=-=-=-=-=-=-=-=-=
Warning: Password input may be echoed.
Password: =-=-=-=-=-=-=-=-=-=-=-=-=-=
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 Layer3Switch-1 192.168.8.201
正在创建 VLAN :201
正在创建 VLAN :202
正在创建 VLAN :203
正在创建 VLAN :204
正在创建 VLAN :205

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-05-30 23:56:33.
<LSW1>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<LSW1>sys
Enter system view, return user view with Ctrl+Z.
[LSW1]vlan 201
[LSW1-vlan201]desc Python_Vlan201
[LSW1-vlan201]vlan 202
[LSW1-vlan202]desc Python_Vlan202
[LSW1-vlan202]vlan 203
[LSW1-vlan203]desc Python_Vlan203
[LSW1-vlan203]vlan 204
[LSW1-vlan204]desc Python_Vlan204
[LSW1-vlan204]vlan 205
[LSW1-vlan205]desc Python_Vlan205
[LSW1-vlan205]return
<LSW1>save
The current configuration will be written to the device.
Are you sure to continue?[Y/N]Y
Info: Please input the file name ( *.cfg, *.zip ) [vrpcfg.zip]:
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 Layer3Switch-2 192.168.8.202
正在创建 VLAN :201
正在创建 VLAN :202
正在创建 VLAN :203
正在创建 VLAN :204
正在创建 VLAN :205

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-05-30 23:56:46.
<Huawei>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]vlan 201
[Huawei-vlan201]desc Python_Vlan201
[Huawei-vlan201]vlan 202
[Huawei-vlan202]desc Python_Vlan202
[Huawei-vlan202]vlan 203
[Huawei-vlan203]desc Python_Vlan203
[Huawei-vlan203]vlan 204
[Huawei-vlan204]desc Python_Vlan204
[Huawei-vlan204]vlan 205
[Huawei-vlan205]desc Python_Vlan205
[Huawei-vlan205]return
<Huawei>save
The current configuration will be written to the device.
Are you sure to continue?[Y/N]Y
Info: Please input the file name ( *.cfg, *.zip ) [vrpcfg.zip]:
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 Layer3Switch-3 192.168.8.203
正在创建 VLAN :201
正在创建 VLAN :202
正在创建 VLAN :203
正在创建 VLAN :204
正在创建 VLAN :205

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-05-30 23:56:59.
<Huawei>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]vlan 201
[Huawei-vlan201]desc Python_Vlan201
[Huawei-vlan201]vlan 202
[Huawei-vlan202]desc Python_Vlan202
[Huawei-vlan202]vlan 203
[Huawei-vlan203]desc Python_Vlan203
[Huawei-vlan203]vlan 204
[Huawei-vlan204]desc Python_Vlan204
[Huawei-vlan204]vlan 205
[Huawei-vlan205]desc Python_Vlan205
[Huawei-vlan205]return
<Huawei>save
The current configuration will be written to the device.
Are you sure to continue?[Y/N]Y
Info: Please input the file name ( *.cfg, *.zip ) [vrpcfg.zip]:
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 Layer3Switch-4 192.168.8.204
正在创建 VLAN :201
正在创建 VLAN :202
正在创建 VLAN :203
正在创建 VLAN :204
正在创建 VLAN :205

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-05-30 23:57:12.
<Huawei>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]vlan 201
[Huawei-vlan201]desc Python_Vlan201
[Huawei-vlan201]vlan 202
[Huawei-vlan202]desc Python_Vlan202
[Huawei-vlan202]vlan 203
[Huawei-vlan203]desc Python_Vlan203
[Huawei-vlan203]vlan 204
[Huawei-vlan204]desc Python_Vlan204
[Huawei-vlan204]vlan 205
[Huawei-vlan205]desc Python_Vlan205
[Huawei-vlan205]return
<Huawei>save
The current configuration will be written to the device.
Are you sure to continue?[Y/N]Y
Info: Please input the file name ( *.cfg, *.zip ) [vrpcfg.zip]:
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 Layer3Switch-5 192.168.8.205
正在创建 VLAN :201
正在创建 VLAN :202
正在创建 VLAN :203
正在创建 VLAN :204
正在创建 VLAN :205

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-05-30 23:57:24.
<Huawei>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]vlan 201
[Huawei-vlan201]desc Python_Vlan201
[Huawei-vlan201]vlan 202
[Huawei-vlan202]desc Python_Vlan202
[Huawei-vlan202]vlan 203
[Huawei-vlan203]desc Python_Vlan203
[Huawei-vlan203]vlan 204
[Huawei-vlan204]desc Python_Vlan204
[Huawei-vlan204]vlan 205
[Huawei-vlan205]desc Python_Vlan205
[Huawei-vlan205]return
<Huawei>save
The current configuration will be written to the device.
Are you sure to continue?[Y/N]Y
Info: Please input the file name ( *.cfg, *.zip ) [vrpcfg.zip]:

进程已结束,退出代码0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

期待未来的男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值