Paramiko sftp.get将日期附加到保存的文件

时间:2019-06-20 19:17:05

标签: python

我正在尝试使用Paramiko从远程节点提取文件,并在本地保存,并将月,日和年附加到已保存的文件中。

到目前为止,这是我想出的,但是如何实现这一点却有些茫然。

#!/usr/bin/python

import paramiko
import os
import datetime
import sys

# variables defined here, should probably reference an external file instead, hardcoded for now
switch_ip = '10.175.2.17'  
username = 'username'
password = 'password'
datestamp = datetime.datetime.now()

try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())    
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())    
    client.connect(switch_ip, username=username, password=password)

    sftp = client.open_sftp()
    sftp.get('primary.cfg', '/backups/nodes/DC1/primary.cfg' + '.' (datestamp.strftime("%m%d%y")))

finally:
    client.close()

1 个答案:

答案 0 :(得分:0)

我认为您忘记了加号(+)

更改

sftp.get('primary.cfg', '/backups/nodes/DC1/primary.cfg' + '.' (datestamp.strftime("%m%d%y")))

sftp.get('primary.cfg', '/backups/nodes/DC1/primary.cfg' + '.'  + (datestamp.strftime("%m%d%y")))