我如何找到使用 ssh 和 rsync 的方法

how do I find a way to use ssh and rsync

我在使用实现 ssh 和 rsync 时遇到了麻烦,包括 python 中的私钥,包括 Popen(子进程)。

基本上要使用的 rsync 语法应该是:

$ rsync -az -e --log-file=$logdir/logfile.out \
'ssh -e /home/user/.ssh/id_rsa' user@server:/target-directory

我的是这样的:

import subprocess

首先,我构建我的 logdir 路径 - 带有变量:

logdir = [basedir + '/' + 'log' + '/' + today + '/' + 'pfdcopy_' \
+ typ + '_' + ts + '.log.txt']

然后我建立目标目录:

target= ['jboss' + '@' + targetsvr + ':' + /data']

最后,我尝试运行这段代码

p1 = subprocess.Popen(['rsync', '-az', '--log-file=%s' % \
logdir/logfile.out , '-e', 'ssh', '-i', \
'/home/user/.ssh/id_rsa', target])

这很复杂,我知道,主要是因为变量和引号。

运行 这个,我总是得到 p1 的不同语法错误。

非常感谢任何帮助。谢谢!

已编辑 (08-10-2018):

这是我完整的 运行 可用代码片段 -

from datetime import datetime
import subprocess
import os
import fnmatch

now = datetime.now()
today = now.strftime("%Y-%m-%d")
ts = now.strftime("%Y-%m-%d-%H-%M-%S")
sign_output_dir = '/Users/fanta4/Documents/python-files/outgoing'
mandator = 'BTV'
formsize = 'A4'
basedir = '/Users/fanta4/Documents'
pdf_to_send = []
targetsvr = 'nas1'

doktyp = (
'WPGEBUEHR', 'WPDURCHFU', 'WPABR', 'WPABRKF', 'WPABRVK', 'WPABRTILG', 'WPABRERTR', 'WPAMIS', 'WPSTREP',
'WPABLAUF', 'WPAVISO', 'WPAUSZUG', 'WPERTRAEG', 'WPSIKTEST', 'WPTRANS', 'WPANSCHAFF', 'KKKONTOMIT', 'KRKURSUEW',
'WPVERLUSTA', 'WPVERLUSTG')


os.chdir(sign_output_dir)
for file in os.listdir(sign_output_dir):
    if fnmatch.fnmatch(file, '*.pdf'):
        pdf_to_send.append(file)

os.chdir(sign_output_dir)
print('debug: doktyp ist: {}'.format(formsize))
for typ in doktyp:
    if typ in str(pdf_to_send):
        ts = now.strftime("%Y-%m-%d-%Hh-%Mm-%Ss")
        print('typ: {:12s} exists -> will be transfered to nas1'.format(typ))
        logdir = [basedir + '/' + 'log' + '/' + mandator + '/' + today + '/' + 'pfdcopy_' + typ + '_' + ts + '.log.txt']
        target = ['jboss' + '@' + targetsvr + '/data' + '/' + mandator + typ]
        p1 = subprocess.Popen(
            ['rsync', '-az', '--log-file=%s' % logdir, '-e', 'ssh', '-i', '/Users/fanta4/.ssh/id_rsa', typ, '-l', target])
        p1.returncode
        if p1 > 0:
            print('debug: Error with rsync of typ: {} to target: {}'.format(typ, targetsvr))
        else:
            print('debug: rsync mandator: {:3s} with typ: {:12s} succeeded'.format(mandator, typ))
    else:
        print('debug: typ: {:12s} does not exist'.format(typ))

logfile = ['/data' + '/' + 'log' + '/' + mandator + '/' + ts]
print('debug: pls see logfile in: {}'.format(logfile))

如果我运行这个代码,我得到:

/Users/fanta4/anaconda3/bin/python "/Users/fanta4/Library/Mobile Documents/com~apple~CloudDocs/entw/python/prog/rsync-test.py"
Traceback (most recent call last):
/Users/fanta4/Documents/python-files/outgoing
debug: doktyp ist: A4
File "/Users/fanta4/Library/Mobile
Documents/com~apple~CloudDocs/entw/python/prog/rsync-test.py", line 37, in <module>
typ: WPGEBUEHR    exists -> will be transfered to nas1
['rsync', '-az', '--log-file=%s' % logdir, '-e', 'ssh', '-i',   '/Users/fanta4/.ssh/id_rsa', typ, '-l', target])
  File "/Users/fanta4/anaconda3/lib/python3.6/subprocess.py", line 709,   in __init__
restore_signals, start_new_session)
File "/Users/fanta4/anaconda3/lib/python3.6/subprocess.py", line 1275, in _execute_child
restore_signals, start_new_session, preexec_fn)
TypeError: expected str, bytes or os.PathLike object, not list

Process finished with exit code 1

你没有提到你遇到了什么语法错误。将这些信息包含在内确实对每个人都有好处。我猜它缺少一个字符串参数周围缺少的引号。

p1 = subprocess.Popen([
    'rsync', '-az', '--log-file=%s' % 'logdir/logfile.out',
    '-e', 'ssh', '-i',
    '/home/user/.ssh/id_rsa', target
])

您的问题由以下几行举例说明(当用于生成稍后用作参数向量中的元素的项目时):

logdir = [basedir + '/' + 'log' + '/' + mandator + '/' + today + '/' + 'pfdcopy_' + typ + '_' + ts + '.log.txt']
target = ['jboss' + '@' + targetsvr + '/data' + '/' + mandator + typ]

您将 logdirtarget 定义为 lists(其中只有一个字符串),而它们需要是 字符串.

只要去掉创建列表的方括号,你就会得到字符串:

logdir = basedir + '/log/' + mandator + '/' + today + '/pfdcopy_' + typ + '_' + ts + '.log.txt'
target = 'jboss@' + targetsvr + '/data/' + mandator + typ