实验3 根据文件修改配置

1、在⽣产环境中,交换机的管理 IP 基本不可能像实验环境中这样 201 到 205,有些交换机的管理 IP 甚⾄在不同
的⽹段,这种情况下,我们就不能简单的⽤ for loop 来循环 IP 地址的最后⼀段来登录交换机。这⾥我们要额外
开⼀个⽂本⽂件,把我们需要登录的交换机 IP 全部写进去,然后⽤ for loop 配合 open() 函数来批量登录所有
交换机。(抄书的哈)
2、⽤上⾯的⽅法登录所有交换机,把华为交换机默认的 MSTP 修改成 STP。

import paramiko
import time
import getpass
username = input("Username: ")
password = getpass.getpass("Password: ")
# 此时 ip_list.txt 需要与 lab3.py 在相同的⽂件夹中
f = open('ip_list.txt')
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, look_for_keys=False)
    command = ssh_client.invoke_shell()
    print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
    print('已经成功登陆交换机 LSW-' + ip)
    l = open('command_list.txt','r')
    l.seek(0)
    for line in l.readlines():
        comm = line.strip()
        command.send(comm + '\n')
    time.sleep(2)
    output = command.recv(65535).decode('ASCII')
    print(output)
ssh_client.close()

C:\Users\82127\.conda\envs\pythonProject\python.exe E:/log/python/pythonProject/Demo03.py
Username: python
hahaha i am in 
C:\Users\82127\.conda\envs\pythonProject\lib\getpass.py:101: GetPassWarning: Can not control echo on the terminal.
  return fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
Password: 123asd
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 LSW-192.168.8.201

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-31 00:05:30.
<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]undo info-center enable
Info: Information center is disabled.
[LSW1]stp mode stp
Info: This operation may take a few seconds. Please wait for a moment...
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 LSW-192.168.8.202

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-31 00:05:32.
<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]undo info-center enable
Info: Information center is disabled.
[Huawei]stp mode stp
Info: This operation may take a few seconds. Please wait for a moment...
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 LSW-192.168.8.203

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-31 00:05:35.
<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]undo info-center enable
Info: Information center is disabled.
[Huawei]stp mode stp
Info: This operation may take a few seconds. Please wait for a moment...
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 LSW-192.168.8.204

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-31 00:05:38.
<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]undo info-center enable
Info: Information center is disabled.
[Huawei]stp mode stp
Info: This operation may take a few seconds. Please wait for a moment...
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 LSW-192.168.8.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-31 00:05:40.
<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]undo info-center enable
Info: Information center is disabled.
[Huawei]stp mode stp
Info: This operation may take a few seconds. Please wait for a moment...

进程已结束,退出代码0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

期待未来的男孩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值