0

我是否正在运行以下脚本来备份 Cisco 交换机。它实际上正在工作,但我无法使用今天的日期创建文件夹并将备份文件放入该文件夹中。

我可以创建文件夹但看不到备份文件。如何将备份文件放入今天的目录?

预期输出

/tftpdata/Region1/20210804/southswitch-192.168.1.4_04-08-2021_14:22:22

或者通过从 iplist 调用来为交换机 IP 创建子文件夹。

/tftpdata/Region1/20210804/192.168.1.4/southswitch-192.168.1.4_04-08-2021_14:22:22

/tftpdata/Region1/20210804/192.168.1.5/southswitch-192.168.1.5_04-08-2021_14:22:22

iplist文件包含以下 IP:

192.168.1.4 192.168.1.5

import paramiko
import time
from getpass import getpass
import os, datetime

TNOW = datetime.datetime.now().replace(microsecond=0)
TFORMAT = '{:%d-%m-%Y_%H:%M:%S}'.format(TNOW)

target_dir = '/tftpdata/Region1/'
today = target_dir + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
if not os.path.exists(today):
    os.mkdir(today)
    print ('\n Create folder successfully', today )
else:
    print ( today,'\n Folder already exists' )
os.chown (str(today), 1000,1001)

username = 'admin'
password = 'XXXXXXXXxx'

DEVICE_LIST = open ('iplist')
for RTR in DEVICE_LIST:
    RTR = RTR.strip()
    print ('\n #### Connecting to the device ' + RTR + '####\n' )
    SESSION = paramiko.SSHClient()
    SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    SESSION.connect(RTR,port=22,
                    username=username,
                    password=password,
                    look_for_keys=False,
                    allow_agent=False)
    DEVICE_ACCESS = SESSION.invoke_shell()
    DEVICE_ACCESS.send('copy running-config tftp://10.10.1.12/Region1/' +today+'/southswitch' + RTR + '_'+TFORMAT + '\n\n')
    time.sleep(5)
    time.sleep(1)
    print ('Backup completed for the device ' + RTR + '\n\n')
    SESSION.close()
4

1 回答 1

1

您不必running-config通过 TFTP 传输。您需要做的就是在您的 PC/服务器上创建一个文件并将其保存到您已经创建的文件夹中。

在某些操作系统 (Windows) 上,:文件夹或文件名中不允许使用冒号 ( )。请避免使用它。

这与您想要实现的目标类似:

import os
import time
from datetime import datetime

import paramiko

TNOW = datetime.now().replace(microsecond=0)
TFORMAT = TNOW.strftime("%d-%m-%Y_%H:%M:%S")

target_dir = "tftpdata/Region1/"
today = target_dir + time.strftime("%Y-%m-%d")
now = TNOW.strftime("%H:%M:%S")

if not os.path.isdir(today):  # Changed to os.path.isdir
    os.makedirs(today)
    print(f"\nCreated {today} folder successfully", today)
else:
    print(f"\n{today} folder already exists!")

username = "cisco"
password = "cisco"

DEVICE_LIST = open("iplist")
for RTR in DEVICE_LIST:
    RTR = RTR.strip()
    print(f"\n#### Connecting to the device {RTR} ####\n")
    SESSION = paramiko.SSHClient()
    SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    SESSION.connect(
        RTR,
        port=22,
        username=username,
        password=password,
        look_for_keys=False,
        allow_agent=False,
    )
    
    # Used `exec_command` instead of `send`
    stdin, stdout, stderr = SESSION.exec_command("show running-config")

    with open(f"{today}/{RTR}-running-config.txt", "w") as outfile:
        outfile.write(str(stdout.read().decode("utf-8")))
    print(f"Backup completed for the {RTR}\n")

    SESSION.close()
DEVICE_LIST.close()  # Don't forget to close the iplist file

甚至我的代码的更好版本是:

import os
from datetime import date

import paramiko

today = date.today()
target_dir = f"tftpdata/Region1/{today}"

# Check if the target directory is created
if not os.path.isdir(target_dir):
    os.makedirs(target_dir)
    print(f"\nCreated {target_dir} folder successfully")
else:
    print(f"\n{target_dir} folder already exists!")

# Username and password
usr = "developer"
pwd = "C1sco12345"

# Read `iplist.txt` and create a list of IP Addresses
with open("iplist.txt", "r") as iplist:
    DEVICE_LIST = [ip for ip in iplist.read().splitlines()]

for DEVICE in DEVICE_LIST:
    print(f"\n#### Connecting to {DEVICE} ####\n")
    # Use `with` statement (Context Manager) to automatically close the connection
    with paramiko.SSHClient() as SESSION:
        SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        SESSION.connect(
            DEVICE,
            port=22,
            username=usr,
            password=pwd,
            look_for_keys=False,
            allow_agent=False,
        )
        
        # Sending command by `exec_command`
        stdin, stdout, stderr = SESSION.exec_command("show running-config")

        with open(f"{target_dir}/{DEVICE}-running-config-{today}.txt", "w") as outfile:
            outfile.write(stdout.read().decode("utf-8").lstrip().replace("\n", ""))

    print(f"Backup completed for {DEVICE}\n")

更新

由于 switch 类型是sg300,并且它已经在netmiko. netmiko使用如下所示做同样的事情:

import csv
import os
from datetime import date

from netmiko import ConnectHandler

today = date.today()
target_dir = f"tftpdata/Region1/{today}"

# Check if the target directory is created
if not os.path.isdir(target_dir):
    os.makedirs(target_dir)
    print(f"\nCreated {target_dir} folder successfully")
else:
    print(f"\n{target_dir} folder already exists!")

# Username and password
usr = "developer"
pwd = "C1sco12345"

# Read `iplist.csv` and create a list of IP Addresses
# Notice here iplist is a CSV file not text file
with open(file="iplist.csv", mode="r") as iplist:
    reader = csv.reader(iplist)
    DEVICE_LIST = [
        {
            "device_type": "cisco_s300",
            "ip": ip.strip(),
            "username": usr,
            "password": pwd,
            "fast_cli": False,
        }
        for ip in iplist
    ]

for DEVICE in DEVICE_LIST:
    print(f'\n#### Connecting to {DEVICE["ip"]} ####\n')
    with ConnectHandler(**DEVICE) as ssh_conn:
        # You don't need to send `terminal length 0`. netmiko handles this for you
        running_config = ssh_conn.send_command("show running-config")

    with open(
        f'{target_dir}/{DEVICE["ip"]}-running-config-{today}.txt', "w"
    ) as outfile:
        outfile.write(running_config.lstrip())
    print(f'Backup completed for {DEVICE["ip"]}\n')

iplist.csv

iplist

于 2021-08-04T13:24:48.997 回答