且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用第二个远程主机 scp

更新时间:2023-12-03 09:50:16

我不知道有什么方法可以在一个命令中直接复制文件,但是如果您可以承认在后台运行 SSH 实例以保持端口转发隧道打开,然后您可以在一个命令中复制文件.

I don't know of any way to copy the file directly in one single command, but if you can concede to running an SSH instance in the background to just keep a port forwarding tunnel open, then you could copy the file in one command.

像这样:

# First, open the tunnel
ssh -L 1234:remote2:22 -p 45678 user1@remote1
# Then, use the tunnel to copy the file directly from remote2
scp -P 1234 user2@localhost:file .

请注意,您在实际的 scp 命令中以 user2@localhost 身份连接,因为第一个 ssh 在 localhost 上的端口 1234 上实例正在监听到 remote2 的转发连接.另请注意,您不需要为每个后续文件副本运行第一个命令;你可以简单地让它运行.

Note that you connect as user2@localhost in the actual scp command, because it is on port 1234 on localhost that the first ssh instance is listening to forward connections to remote2. Note also that you don't need to run the first command for every subsequent file copy; you can simply leave it running.