使用来自本地机器的一个命令通过代理进行 Scp?

grm*_*grm 61 scp

我有我的 local.machine、proxy.machine 和 target.machine。local.machine与target.machine没有直接联系,但需要通过proxy.machine。

我想将一个文件从 target.machine scp 到 local.machine。仅使用来自 local.machine 的一个命令就可以做到这一点吗?

use*_*925 70

你可以用ProxyJump做到这一点。把它放在你的~/.ssh/config文件中(如果文件不存在则创建文件):

Host target.machine
User targetuser
HostName target.machine
ProxyJump proxyuser@proxy.machine
Run Code Online (Sandbox Code Playgroud)

保存文件后,您可以使用

ssh target.machine
Run Code Online (Sandbox Code Playgroud)

任何时候你想连接。Scp 也可以工作,因为它也尊重 ssh 配置文件。如果您使用的是 GNOME 并且想要使用 GUI,那么 Nautilus 也是如此。

旧答案(适用于旧版本的 OpenSSH)

Host target.machine
User targetuser
HostName target.machine
ProxyCommand ssh proxyuser@proxy.machine nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

  • 可能有帮助的 3 件事,1) 如果您还在同一个 `~/.ssh/config` 文件中显示 `Host proxy.machine` 行,这会有所帮助,2) 提及这些命令是否可以嵌套(即客户端连接代理主机 1 连接到代理主机 2,代理主机 2 连接到...目标)和 3)什么是 `nc %h %p` (7认同)
  • 只是一个快速的笔记。如果您通过 ssh 的 -i 命令行选项使用替代身份文件,则需要为 ProxyCommand 配置和 ssh 命令行指定该选项。 (6认同)

Cup*_*Tae 30

您现在可以* 以单行方式执行此操作,无需 nc任何地方:

scp -o "ProxyCommand ssh pcreds@proxy.machine -W %h:%p" tcreds@target.machine:file .
Run Code Online (Sandbox Code Playgroud)

解释

pcreds并且tcreds如果需要的话代表了你的代理和目标凭据(usernameusername:password,等)。

这是可能的,因为内置了类似 netcat 的能力,消除了nc对中间主机的要求。使用ssh -W host:port设置到指定主机和端口的隧道并将其连接到标准输入/标准输出,然后scp在隧道上运行。

%h%pProxyCommand被来自外部的目标主机和端口scp的命令,以节省您不必重复它们。

为了更方便,您可以在 ssh 配置中配置代理:

Host target.machine
    ProxyCommand ssh pcreds@proxy.machine -W %h:%p
Run Code Online (Sandbox Code Playgroud)

从那时起就做

scp tcreds@target.machine:file .
Run Code Online (Sandbox Code Playgroud)

*自 OpenSSH 5.4 - 2010 年 3 月发布


小智 20

如果您不介意使用 rsync 而不是 scp,则可以使用以下单行:

rsync -v --rsh "ssh proxy.machine ssh" target.machine:/remote/file /local/dir/
Run Code Online (Sandbox Code Playgroud)

(您需要无密码访问代理机器)


Hol*_*ust 19

您可以通过一个命令来完成,但您需要在代理机器上安装 netcat (nc):

ssh -o "ProxyCommand ssh poxyuser@proxy.machine nc -w 1 %h 22" targetuser@target.machine
Run Code Online (Sandbox Code Playgroud)

[编辑:混淆机器的顺序...]


小智 8

$ ssh -f -N -L <localport>:<target.machine:port> user@proxy.machine
$ scp target-user@local.machine:/remote/file -P <localport> .
Run Code Online (Sandbox Code Playgroud)

好吧,实际上是两个命令...


wha*_*ley 7

单线?没有离开我的头顶。你需要先建立一个代理,你不能用 scp 本身来做到这一点。

手动执行时,我为我的隧道打开了一个屏幕会话:

screen -S tunnel

屏幕用于保持隧道在后台外壳中运行。使用您希望在后台保持隧道打开的任何技术(@weeheavy 的答案可能是最简单的)。一旦进入屏幕会话,我就会像这样开始我的隧道

ssh -L 2222:target.machine:22 [user@]proxy.machine

分解一下,基本上是说“在我的本地机器上,打开端口 2222,任何连接到 localhost:2222 的连接都通过 proxy.machine 代理到 target.machine:22”

建立 ssh 连接和隧道后,使用“Ca d”从屏幕会话中分离。要返回该屏幕会话,请键入screen -raAd tunnel

回到原始 shell 后,您的 scp 命令将如下所示

scp -P 2222 localhost:your/file/on/target.machine local/path

请记住,本地主机端口 2222 实际上只是一个通往 target.machine 的隧道。