我有一台服务器“B”,可以将 SCP 文件发送到服务器“A”或从服务器“A”发送 SCP 文件,也可以将 SCP 文件发送到服务器“C”或从服务器“C”发送 SCP 文件。
即 A <-----> B <-----> C
服务器“A”和服务器“C”无法相互访问。只有服务器 B 可以到达两者。我想将文件从 A 传输到 C,而服务器 B 上没有(或最少)存储。
有没有一种方法可以将文件从 A 传输到 C,而不将其存储在 B 中或只需最少的步骤?
谢谢。
从scp(1):
描述
...两个远程主机之间的复制也是允许的。
scp host1:foo.txt host2:foo.txt
Run Code Online (Sandbox Code Playgroud)
如果您愿意,您可以在不使用 scp 的情况下执行此操作。登录机器“B”并运行以下命令:
ssh userA@A 'cat /source/file' | ssh userC@C 'cat > /dest/file'
Run Code Online (Sandbox Code Playgroud)
您应该将这些 ssh 实例中的一个或两个设置为使用密钥登录,这样两个 ssh 实例就不会同时提示您输入密码。
如果您希望文件复制过程更加防错,或者如果您想一次传输多个文件,您可以使用tar:
ssh userA@A 'cd /source/dir && tar cf - file1 file2...' |
ssh userC@C 'cd /dest/dir && tar xvf -'
Run Code Online (Sandbox Code Playgroud)
如果您更愿意从 A 运行命令,那么类似这样的操作应该有效:
tar cf - file... | ssh userB@B 'ssh userC@C "cd /dest/dir && tar xvf -" '
Run Code Online (Sandbox Code Playgroud)