DELL S4048-ON交换机:Python远程自动备份配置信息

import threading
import paramiko
import subprocess
import sys
import time
import datetime


ips = ["10.20.1.240", "172.16.100.2", "172.16.101.2"]
user = "用户名"
passwd = "密码"
client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for ip in ips:
    client.connect(ip, 22, username=user, password=passwd)
    remote_connection = client.invoke_shell()
    output = remote_connection.recv(1000)
    time.sleep(1)
    print(output)

    time.sleep(1)
    remote_connection.send("enable\n");
    output1 = remote_connection.recv(5000)
    print(output1)

    time.sleep(1)
    remote_connection.send("sys密码\n");
    output2 = remote_connection.recv(5000)
    print(output2)

    time.sleep(1)
    remote_connection.send("copy startup-config tftp://10.20.0.201/dell-%s.txt\n" % ip);
    output3 = remote_connection.recv(5000)
    time.sleep(5)
    print(output3)

测试

# python tftp_dell.py 
b'DELL_S4048T_2haolou>'
b'enable\r\n'
b'Password: '
b"\r\n\r\n\r\n The SupportAssist EULA acceptance option has not been selected. SupportAssist\r\n can be enabled once the SupportAssist EULA has been accepted. Use the:\r\n 'support-assist activate' command to accept EULA and enable SupportAssist.\r\n\r\nDELL_S4048T_2haolou#"
b'DELL_S4048T_5haolou_#1>'
b'enable\r\n'
b'Password: '
b"\r\n\r\n\r\n The SupportAssist EULA acceptance option has not been selected. SupportAssist\r\n can be enabled once the SupportAssist EULA has been accepted. Use the:\r\n 'support-assist activate' command to accept EULA and enable SupportAssist.\r\n\r\nDELL_S4048T_5haolou_#1#"
b'DELL_S4048T_1haolou_#1>'
b'ena'
b'ble\r\nPassword: '
b"\r\n\r\n\r\n The SupportAssist EULA acceptance option has not been selected. SupportAssist\r\n can be enabled once the SupportAssist EULA has been accepted. Use the:\r\n 'support-assist activate' command to accept EULA and enable SupportAssist.\r\n\r\nDELL_S4048T_1haolou_#1#"


# ll /tftpboot/dell-*.txt 
-rw-rw-rw-. 1 nobody nobody 15450 7月   3 23:40 /tftpboot/dell-10.20.1.240.txt
-rw-rw-rw-. 1 nobody nobody 15074 7月   3 23:40 /tftpboot/dell-172.16.100.2.txt
-rw-rw-rw-. 1 nobody nobody 14369 7月   3 23:40 /tftpboot/dell-172.16.101.2.txt

其他交换机的tftp 备份配置信息的命令:

Huawei S12708:

tftp 10.20.0.201 put flash:/vrpcfg.zip vrpcfg.zip

DELL S4048T:

copy startup-config tftp://10.20.0.201/DELL_S4048T_startup-config

H3C S7506E:

tftp 10.20.0.201 put flash:/startup.cfg startup.cfg

Huawei S5720-32X-EI-AC:

tftp 10.20.0.201 put flash:/vrpcfg.zip vrpcfg.zip

H3C S7003E:

tftp 10.20.0.201 put flash:/startup.cfg tartup.cfg

改进版:多线程并行备份:

import threading
import paramiko
from time import sleep, ctime


ips = ["10.20.1.240", "172.16.100.2", "172.16.101.2"]
user = "用户名"
passwd = "密码"


def dell_tftp(ip):
    client = paramiko.SSHClient()

    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip, 22, username=user, password=passwd)
    remote_connection = client.invoke_shell()
    # output = remote_connection.recv(1000)
    # sleep(1)
    # print(output)

    sleep(1)
    remote_connection.send("enable\n")
    # output1 = remote_connection.recv(5000)
    # print(output1)

    sleep(1)
    remote_connection.send("enable密码\n")
    # output2 = remote_connection.recv(5000)
    # print(output2)

    sleep(1)
    remote_connection.send(
        "copy startup-config tftp://10.20.0.201/dell-%s.txt\n" %
        ip)
    # output3 = remote_connection.recv(5000)
    sleep(2)
    # print(output3)


def main():
    print('starting at:', ctime())
    threads = []
    nloops = range(len(ips))

    for i in nloops:
        t = threading.Thread(target=dell_tftp, args=(ips[i],))
        threads.append(t)

    for i in nloops:
        threads[i].start()

    for t in threads:
        t.join()

    print('all DONE at:', ctime())

if __name__ == '__main__':
    main()

运行测试结果:

# python tftp_dell.py 
starting at: Sun Jul 14 22:36:11 2019
all DONE at: Sun Jul 14 22:36:18 2019
# ls /tftpboot/dell*.txt -la
-rw-rw-rw-. 1 nobody nobody 15450 7月  14 22:36 /tftpboot/dell-10.20.1.240.txt
-rw-rw-rw-. 1 nobody nobody 15074 7月  14 22:36 /tftpboot/dell-172.16.100.2.txt
-rw-rw-rw-. 1 nobody nobody 14369 7月  14 22:36 /tftpboot/dell-172.16.101.2.txt
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注