ProxyCommand 用于多跳和提示身份验证

1.6*_*803 21 ssh

如何用 重写以下命令ProxyCommand

ssh -l username1 -t jumphost1 \
ssh -l username2 -t jumphost2 \
ssh -l username3 -t jumphost3 \
ssh -l username4    server
Run Code Online (Sandbox Code Playgroud)

这不起作用

ssh -o ProxyCommand="\
ssh -l username1 -t jumphost1  \
ssh -l username2 -t jumphost2  \
ssh -l username3 -t jumphost3" \
    -l username4    server

username1@jumphost1's password:
Pseudo-terminal will not be allocated because stdin is not a terminal.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
ssh_exchange_identification: Connection closed by remote host
Run Code Online (Sandbox Code Playgroud)

我知道它ncscp. 我检查了ssh_config手册页,但至少对我而言,信息非常稀缺。

编辑

我尝试按照下面的建议使用ProxyCommand嵌套在另一个中ProxyCommand,但我总是得到以下内容

debug3: ssh_init_stdio_forwarding: 192.17.2.2:2222
debug1: channel_connect_stdio_fwd 192.17.2.2:2222
debug1: channel 0: new [stdio-forward]
debug2: fd 4 setting O_NONBLOCK
debug2: fd 5 setting O_NONBLOCK
debug1: getpeername failed: Bad file descriptor
debug3: send packet: type 90
debug2: fd 3 setting TCP_NODELAY
debug3: ssh_packet_set_tos: set IP_TOS 0x10
debug1: Requesting no-more-sessions@openssh.com
debug3: send packet: type 80
debug1: Entering interactive session.
Run Code Online (Sandbox Code Playgroud)

幸运的是,因为7.3 -J或达到了ProxyJump我的目的——尽管我仍然必须解决我的密钥设置问题。

ssh -q -J user1@jumphost1,user2@jumphost2,user3@jumphost3 user@server
Run Code Online (Sandbox Code Playgroud)

Jak*_*uje 29

nc不再推荐该版本。使用-W最新版本的 OpenSSH 中提供的开关。此外,您不需要将配置复制到其他主机!所有配置都需要在您的主机上完成,并且不会scp以任何方式干扰。只需创建一个文件~/.ssh/config

Host jumphost1
  User username1
Host jumphost2
  User username2
  ProxyCommand ssh -W %h:%p jumphost1
Host jumphost3
  User username3
  ProxyCommand ssh -W %h:%p jumphost2
Host server
  User username4
  ProxyCommand ssh -W %h:%p jumphost3
Run Code Online (Sandbox Code Playgroud)

然后使用ssh server或使用连接scp file server:path/。如果你坚持使用 oneliner(或者不确定你对ProxyCommand嵌套的意思),那么正如已经指出的那样,这是逃生的地狱:

ssh -oProxyCommand= \
  'ssh -W %h:%p -oProxyCommand= \
    \'ssh -W %h:%p -oProxyCommand= \
      \\\'ssh -W %h:%p username1@jumphost1\\\' \
    username2@jumphost2\' \
  username3@jumphost3' \
username4@server
Run Code Online (Sandbox Code Playgroud)

你基本上需要从里面去。

  • 感谢@Jakuje 为了完整起见,我在这里提供了`src->jmp1->jmp2->dev` 路径的完整命令,其中命令在`src` 上发出,目标机器是`dev``ssh -o "ProxyCommand=ssh -W %h:%p -o 'ProxyCommand=ssh -W %%h:%%p root@jmp1' root@jmp2" admin@dev` (3认同)
  • @RomanDodin double `%` 应该可以工作:`%%h` 在你的情况下 (2认同)

Dop*_*oti -1

我已经用跳完成了此操作,但它应该适用于三跳。最简单的方法是~/.ssh/config在每个主机上设置该文件。因此,如果您想打开hosta并访问hostdviahostb和 hostc`,您可以这样设置您的配置:

hosta:~/.ssh/config

Host hostd
    User username
    ProxyCommand ssh hostb nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

hostb:~/.ssh/config

Host hostd
    User username
    ProxyCommand ssh hostc nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

hostc:~/.ssh/config

Host hostd
    User username
    ProxyCommand ssh hostd nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

然后,您可以ssh hostd在链中的任何主机上访问hostd.

使用 netcat 作为代理不会干扰scp.

如果由于某种原因您确实不想使用本地~/.ssh/config文件,您可以执行以下操作hosta

ssh -oProxyCommand='ssh -oProxyCommand=\'ssh -o ProxyCommand=\\\'ssh username@hostd nc %h %p 2>/dev/null\\\' username@hostc nc %h %p 2> /dev/null' username@hostb nc %h %p 2> /dev/null' username@hostd
Run Code Online (Sandbox Code Playgroud)