I am writing part of the software that has to execute remote shell command. This command waits for some additional info from stdin in order to continue it's execution.
我正在编写必须执行远程shell命令的软件的一部分。此命令等待来自stdin的一些其他信息以继续执行。
Here is the remote program test_interactive.sh:
这是远程程序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
At this moment I am initiating the execution of my command with SSH_MSG_CHANNEL_REQUEST exec message. Then after window adjustments and success message, I am sending SSH_MSG_CHANNEL_DATA with the data that I'd like to be pushed into my program's stdin by sshd server. As it turned out, my program doesn't receive anything on stdin.
此时我正在使用SSH_MSG_CHANNEL_REQUEST exec消息启动执行命令。然后在窗口调整和成功消息之后,我发送SSH_MSG_CHANNEL_DATA,其中包含我希望被sshd服务器推送到程序的stdin中的数据。事实证明,我的程序在stdin上没有收到任何东西。
SSH_MSG_CHANNEL_REQUEST exec causes sshd process to spawn two additional processes sshd(63023) and test_interactive(63024).
SSH_MSG_CHANNEL_REQUEST exec导致sshd进程生成另外两个进程sshd(63023)和test_interactive(63024)。
The process test_interactive*(63024) has the following file descriptors:
进程test_interactive *(63024)具有以下文件描述符:
And here is the sequence of messages that leads me to the state, where I'm sending messages and receiving back nothing from my program:
这里有一系列消息引导我进入状态,我正在发送消息并从我的程序中收回任何信息:
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
How should I instruct sshd to push data into stdin that it receives with SSH_MSG_CHANNEL_DATA? I have tried to find it out by looking at the source code of sshd, but it's quite big and I think it will take weeks for me to figure this out :)
我应该如何指示sshd将数据推送到使用SSH_MSG_CHANNEL_DATA接收的stdin?我试着通过查看sshd的源代码找到它,但是它非常大,我想我需要花费数周的时间来解决这个问题:)
Combination of requests that finally does it
The daemon sshd pushes data into stdin if you do the following:
如果执行以下操作,守护程序sshd会将数据推送到stdin:
- Immediately after 'SSH_MSG_CHANNEL_REQUEST exec' you send 'SSH_MSG_CHANNEL_DATA Hello World'
- Immediately after 'SSH_MSG_CHANNEL_DATA Hello World' you send 'SSH_MSG_CHANNEL_EOF'
在'SSH_MSG_CHANNEL_REQUEST exec'之后立即发送'SSH_MSG_CHANNEL_DATA Hello World'
在'SSH_MSG_CHANNEL_DATA Hello World'之后,您立即发送'SSH_MSG_CHANNEL_EOF'
Or more illustratively:
或者更说明一下:
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'
This doesn't solve the problem, but gives a little bit more information on the sshd's behaviour. I still need to be able to form my message for the program based on what has come to me, not just fire and forget.
这并没有解决问题,但提供了有关sshd行为的更多信息。我仍然需要能够根据发生在我身上的事情形成我对该程序的信息,而不仅仅是开火和忘记。
By the way, it would be cool to add rfc4254 tag ;) ↓
顺便说一句,添加rfc4254标签会很酷;)↓
1 个解决方案
#1
0
With the help of the Kenster I have found the answer.
在Kenster的帮助下,我找到了答案。
Yes, sshd sends data into stdin after 'SSH_MSG_CHANNEL_REQUEST exec'.
But you need to be careful with what you are sending. In my case, the methods that formed the response message erased '\n' character from the stream. This character was needed for shell function read to proceed.
但是你需要小心你发送的东西。在我的例子中,形成响应消息的方法从流中删除了'\ n'字符。 shell函数读取需要此字符才能继续。
#1
0
With the help of the Kenster I have found the answer.
在Kenster的帮助下,我找到了答案。
Yes, sshd sends data into stdin after 'SSH_MSG_CHANNEL_REQUEST exec'.
But you need to be careful with what you are sending. In my case, the methods that formed the response message erased '\n' character from the stream. This character was needed for shell function read to proceed.
但是你需要小心你发送的东西。在我的例子中,形成响应消息的方法从流中删除了'\ n'字符。 shell函数读取需要此字符才能继续。