ssh 与我的自定义 shell 一起工作,但不能直接使用 ssh 执行命令

ssh works with my custom shell but can't execute commands directly using ssh

我在网站上搜索,但找不到类似的问题和解决方案。所以问了一个新问题。 我正在尝试使用一些有限的命令构建自定义 shell。作为起点,我使用由 Stephen Brennan 提供的 this custom shell。它有3个内置命令(cd、help、exit),也可以执行系统命令。

我在 /etc/passwd 中编辑了这一行:

root:x:0:0:root:/root:/mnt/n1/custom-shell

并将此行添加到 /etc/shells:

/mnt/n1/custom-shell

现在我可以使用 ssh 连接到远程主机,我的自定义 shell 出现,我可以在其中输入命令;但我不能直接使用 ssh 执行命令。例如,当我尝试使用 ssh 192.168.32.1 help 在远程主机上执行 运行 "help" 命令时,没有任何反应。我试了ssh -v 192.168.32.1 help,结果如下。它停留在 debug1: Sending command: help.

OpenSSH_4.3p2, OpenSSL 0.9.8b 04 May 2006
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.32.1 [192.168.32.1] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/id_rsa type 1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: loaded 3 keys
debug1: Remote protocol version 1.99, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host '192.168.32.1' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:5
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug1: Offering public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: password
root@192.168.32.1's password:
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: help

因此我无法使用 scp -v devel/bin/i2c root@192.168.32.1:/mnt/n1/ 将文件复制到远程主机,它停留在这一行:

debug1: Sending command: scp -v -t /mnt/n1/

我搜索了很多但找不到答案。任何帮助将不胜感激。

调用 shell 并使其成为 运行 命令的标准方法之一是使用命令行参数 -c 和运行 的命令。这是 OpenSSH SSH 服务器用来调用客户端请求的命令的方法。

当 SSH 客户端连接到 OpenSSH 服务器并请求 运行 服务器上的命令时,服务器最终会调用等效于此的命令:

$SHELL -c 'the-command'

其中 $SHELL 是用户的默认值 shell,-c 是文字命令行选项,the-command 是客户端请求的命令。该命令作为单个命令行参数传递给 shell。

在您的情况下,shell 将是您的自定义 shell 程序。它将需要解析其命令行参数,以了解它是带有“-c”和命令字符串的 运行 的事实。然后它必须执行在命令行上指定的命令,而不是从输入流中读取命令。

与任何其他 C 程序一样,shell 程序的入口点是一个名为 main:

的函数
int main(int argc, char **argv)
{
    ...

当您的 shell 以这种方式从 SSH 服务器调用时,您应该会发现 argcargv 将反映命令行参数 ["/name/of-shell", "-c", "the-command"]。您可以调用 getopt(3) 等标准函数来帮助解析命令行参数。

我要补充一点,scp 的工作原理是让服务器通过您的 shell 调用一个程序。因此,您会看到您的自定义 shell 被调用(使用 -c 参数和 运行 的命令)用于 scp 会话。您也可能会在 sftp 会话中看到这一点,具体取决于服务器的配置方式。