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