用python的paramiko文本txt批量登陆设备配置

当我们的登陆设备地址不连续的情况下,或者不同网段,我们就不能简单的使用for循环来登陆设备了。
我们要额外建立一个文本文件,把需要登陆的交换机的管理IP地址全部写进去,然后用for循环配合open()函数来读取该文档中的管理地址

云配置

用python的paramiko文本txt批量登陆设备配置_第1张图片

拓扑

用python的paramiko文本txt批量登陆设备配置_第2张图片

交换机配置

以LSW2为例:

interface Vlanif1
 ip address 192.168.56.2 255.255.255.0
#
aaa
 local-user admin password cipher Huawei@123   //创建python用户,密码为123
 local-user admin privilege level 15
 local-user admin service-type ssh
#
user-interface vty 0 4
 authentication-mode aaa
 protocol inbound ssh
#
stelnet server enable
ssh user admin authentication-type all
ssh user admin service-type all
ssh client first-time enable

目的

在多台设备上关闭stp

代码

需要在本地创建一个TXT文件,把需要登陆配置的设备管理IP都放进去
如果该文件与py文件在同一个文件夹会方便很多

ip_list:
192.168.56.2
192.168.56.10
192.168.56.52
import paramiko
import time

username = input('Username: ')
password = input('Password: ')

f = open("ip_list.txt", "r")
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)
    print("Successfully connect to ", ip)
    command = ssh_client.invoke_shell()
    command.send(b"screen-length 0 temporary\n")
    command.send(b"sys\n")
    command.send(b"undo stp enable\n")
    command.send(b"Y\n")
    command.send(b"q\n")
    command.send(b"save\n")
    command.send(b"Y\n")
    time.sleep(1)
    output = command.recv(65535)
    print(output.decode("ascii"))

f.close()
ssh_client.close()

结果

Username: admin
Password: Huawei@123
Successfully connect to  192.168.56.2

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 2023-11-28 22:25:59.
<LSW2>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<LSW2>sys
Enter system view, return user view with Ctrl+Z.
[LSW2]undo stp enable
Warning: The global STP state will be changed. Continue? [Y/N]Y
Info: This operation may take a few seconds. Please wait for a moment...done.
[LSW2]q
<LSW2>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]:
Successfully connect to  192.168.56.10

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 2023-11-28 22:26:01.
<LSW3>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<LSW3>sys
Enter system view, return user view with Ctrl+Z.
[LSW3]undo stp enable
Warning: The global STP state will be changed. Continue? [Y/N]Y
Info: This operation may take a few seconds. Please wait for a moment...done.
[LSW3]q
<LSW3>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]:
Successfully connect to  192.168.56.52

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 2023-11-28 22:26:03.
<LSW4>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<LSW4>sys
Enter system view, return user view with Ctrl+Z.
[LSW4]undo stp enable
Warning: The global STP state will be changed. Continue? [Y/N]Y
Info: This operation may take a few seconds. Please wait for a moment...done.
[LSW4]q
<LSW4>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]:

Process finished with exit code 0

你可能感兴趣的:(网工的python之路,网络)