gpt4 book ai didi

linux - SSH_MSG_CHANNEL_REQUEST exec 之后,sshd 是否将数据发送到 stdin

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:46 25 4
gpt4 key购买 nike

我正在编写必须执行远程 shell 命令的软件的一部分。此命令等待来自 stdin 的一些附加信息,以便继续执行。

这里是远程程序test_interactive.sh:

#!/bin/sh

echo -n "Please, enter a string: "

read string # <-- Wait here for the input to come...
if [ -z "$string" ] ; then
echo "1. You entered empty string." 1>&2
else
echo "1. Your string: $string"
fi

此时我正在使用 SSH_MSG_CHANNEL_REQUEST 执行消息开始执行我的命令。然后在窗口调整和成功消息之后,我将发送 SSH_MSG_CHANNEL_DATA 以及我希望通过 sshd 推送到程序的 stdin 中的数据服务器。事实证明,我的程序在 stdin 上没有收到任何东西。

SSH_MSG_CHANNEL_REQUEST exec 导致 sshd 进程产生两个额外的进程 sshd(63023) 和 test_interactive(63024)。 enter image description here

进程 test_interactive*(63024) 具有以下文件描述符: enter image description here

下面是引导我进入状态的消息序列,在该状态下我正在发送消息但没有从我的程序接收任何内容:

Send:    'SSH_MSG_CHANNEL_OPEN session'
Receive: 'SSH_MSG_CHANNEL_OPEN_CONFIRMATION 0'
Send: 'SSH_MSG_CHANNEL_REQUEST exec'
Receive: 'SSH_MSG_CHANNEL_WINDOW_ADJUST'
Receive: 'SSH_MSG_CHANNEL_SUCCESS'
Receive: 'SSH_MSG_CHANNEL_DATA Please, enter a string:'
Send: 'SSH_MSG_CHANNEL_DATA hello' <-- communication stops here

我应该如何指示 sshd 将数据推送到它通过 SSH_MSG_CHANNEL_DATA 接收的标准输入?我试图通过查看 sshd 的源代码来找到它,但是它非常大,我认为我需要几周的时间才能弄清楚:)

最终完成的请求组合

如果您执行以下操作,守护程序 sshd 会将数据推送到 stdin:

  1. 在“SSH_MSG_CHANNEL_REQUEST exec”后立即发送“SSH_MSG_CHANNEL_DATA Hello World
  2. 在“SSH_MSG_CHANNEL_DATA Hello World”之后立即发送“SSH_MSG_CHANNEL_EOF

或者更形象地说:

Send:    'SSH_MSG_CHANNEL_REQUEST exec'
Send: 'SSH_MSG_CHANNEL_DATA Hello World'
Send: 'SSH_MSG_CHANNEL_EOF'
Receive: 'SSH_MSG_CHANNEL_DATA Please, enter a string:'
Receive: 'SSH_MSG_CHANNEL_REQUEST exit-status'
Receive: 'SSH_MSG_CHANNEL_DATA Please, enter a string: 1. Your string: Hello\n'
Receive: 'SSH_MSG_CHANNEL_EOF'
Receive: 'SSH_MSG_CHANNEL_CLOSE'
Send: 'SSH_MSG_CHANNEL_CLOSE'
Send: 'SSH_MSG_DISCONNECT'

这并没有解决问题,但提供了一些关于 sshd 行为的更多信息。我仍然需要能够根据我遇到的情况为该计划形成我的信息,而不是一劳永逸。

顺便说一下,添加 rfc4254 标签会很酷 ;)↓

最佳答案

Kenster 的帮助下我找到了答案。

是的,sshd 在“SSH_MSG_CHANNEL_REQUEST exec”之后将数据发送到stdin

但是您需要小心发送的内容。在我的例子中,形成响应消息的方法从流中删除了 '\n' 字符。 shell 函数 read 需要此字符才能继续。

关于linux - SSH_MSG_CHANNEL_REQUEST exec 之后,sshd 是否将数据发送到 stdin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com