ssh可与我的自定义外壳一起使用,但不能直接使用ssh执行命令

时间:2018-10-02 08:02:37

标签: shell ssh scp

我搜索了整个网站,但找不到类似的问题和解决方案。于是问了一个新问题。 我正在尝试使用一些有限的命令来构建自定义外壳。首先,我使用的是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连接到远程主机,并且我的自定义外壳出现了,可以在其中输入命令了;但是我无法直接使用ssh执行命令。例如,当我尝试使用ssh 192.168.32.1 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/

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

1 个答案:

答案 0 :(得分:2)

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

当SSH客户端连接到OpenSSH服务器并请求在服务器上运行命令时,服务器最终将调用以下命令:

$SHELL -c 'the-command'

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

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

与其他C程序一样,shell程序的入口点是名为main的函数:

int main(int argc, char **argv)
{
    ...

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

我要补充一点,scp的工作原理是让服务器通过您的Shell调用程序。因此,您将看到针对scp会话调用了自定义外壳程序(带有-c参数和要运行的命令)。对于sftp会话,您可能还会看到此消息,具体取决于服务器的配置方式。