我想知道是否有办法通过remote1主机直接从我的本地机器上通过remote2主机获取SCP文件.
网络仅允许从remote1主机连接到remote2主机.此外,remote1主机和remote2主机都不能scp到我的本地计算机.
是否有类似的东西:
scp user1@remote1:user2@remote2:file .
第一个窗口:ssh remote1然后scp remot2:file ..
第二壳: scp remote1:file .
第一窗口: rm file; logout
我可以写一个脚本来完成所有这些步骤,但如果有直接的方法,我宁愿使用它.
谢谢.
编辑:我正在考虑打开SSH隧道,但我很困惑什么值放在哪里.
目前,要访问remote1,我在$HOME/.ssh/config本地计算机上有以下内容.
Host remote1
User user1
Hostname localhost
Port 45678
Run Code Online (Sandbox Code Playgroud)
一旦打开remote1,要访问remote2,它是标准的本地DNS和端口22.我应该放置remote1和/或更改localhost什么?
Dol*_*000 89
我不知道有什么方法可以直接在一个命令中复制文件,但是如果您可以承认在后台运行SSH实例只是保持端口转发隧道打开,那么您可以在一个命令中复制该文件.
像这样:
# 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 .
Run Code Online (Sandbox Code Playgroud)
请注意,您user2@localhost在实际scp命令中进行连接,因为它位于localhost上的端口1234上,第一个ssh实例正在侦听前向连接remote2.另请注意,您不需要为每个后续文件副本运行第一个命令; 你可以简单地让它运行.
oli*_*bre 67
ssh即使在复杂的情况下,您也可以使用单个命令行处理文件传输,只需使用ssh;-)
如果remote1无法连接到localhost以下内容,这很有用:
ssh user1@remote1 'ssh user2@remote2 "cat file"' > file
Run Code Online (Sandbox Code Playgroud)
tar但是你丢失了文件属性(所有权,权限......).
但是,tar您的朋友是否保留这些文件属性:
ssh user1@remote1 'ssh user2@remote2 "cd path2; tar c file"' | tar x
Run Code Online (Sandbox Code Playgroud)
您还可以压缩以减少网络带宽:
ssh user1@remote1 'ssh user2@remote2 "cd path2; tar cj file"' | tar xj
Run Code Online (Sandbox Code Playgroud)
并且tar还允许您通过基本传输递归目录ssh:
ssh user1@remote1 'ssh user2@remote2 "cd path2; tar cj ."' | tar xj
Run Code Online (Sandbox Code Playgroud)
ionice如果该文件是巨大的,你不想打扰其他重要的网络应用,你可能会错过网络吞吐量的限制提供scp和rsync工具(例如,scp -l 1024 user@remote:file不使用超过1兆比特/秒).
但是,一种解决方法是使用ionice一个命令行:
ionice -c2 -n7 ssh u1@remote1 'ionice -c2 -n7 ssh u2@remote2 "cat file"' > file
Run Code Online (Sandbox Code Playgroud)
注意:ionice旧版本可能无法使用.
Joh*_*nak 29
这样就可以了:
scp -o 'Host remote2' -o 'ProxyCommand ssh user@remote1 nc %h %p' user@remote2:path/to/file .
Run Code Online (Sandbox Code Playgroud)
remote2直接从主机向SCP发送文件,将两个选项(Host和ProxyCommand)添加到〜/ .ssh/config文件中(另请参阅超级用户的此答案).然后你可以运行:
scp user@remote2:path/to/file .
Run Code Online (Sandbox Code Playgroud)
从您的本地机器无需考虑remote1.
使用openssh 7.3及更高版本很容易。在配置文件中使用ProxyJump选项。
# Add to ~/.ssh/config
Host bastion
Hostname bastion.client.com
User userForBastion
IdentityFile ~/.ssh/bastion.pem
Host appMachine
Hostname appMachine.internal.com
User bastion
ProxyJump bastion # openssh 7.3 version new feature ProxyJump
IdentityFile ~/.ssh/appMachine.pem. #no need to copy pem file to bastion host
Run Code Online (Sandbox Code Playgroud)
运行以登录或复制的命令
ssh appMachine # no need to specify any tunnel.
scp helloWorld.txt appMachine:. # copy without intermediate jumphost/bastion host copy.**
Run Code Online (Sandbox Code Playgroud)
当然,如果未在配置文件中配置,则可以使用选项“ -J”向ssh命令指定堡垒跳转主机。
注意到目前为止,scp似乎还不支持“ -J”标志。(我在手册页中找不到。但是上面的scp可用于配置文件设置)