- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写必须执行远程 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)。
进程 test_interactive*(63024) 具有以下文件描述符:
下面是引导我进入状态的消息序列,在该状态下我正在发送消息但没有从我的程序接收任何内容:
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:
或者更形象地说:
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 的帮助下我找到了答案。
但是您需要小心发送的内容。在我的例子中,形成响应消息的方法从流中删除了 '\n' 字符。 shell 函数 read 需要此字符才能继续。
关于linux - SSH_MSG_CHANNEL_REQUEST exec 之后,sshd 是否将数据发送到 stdin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568858/
我正在编写必须执行远程 shell 命令的软件的一部分。此命令等待来自 stdin 的一些附加信息,以便继续执行。 这里是远程程序test_interactive.sh: #!/bin/sh echo
我是一名优秀的程序员,十分优秀!