Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ssh与我的自定义shell一起工作,但不能直接使用ssh执行命令_Shell_Ssh_Scp - Fatal编程技术网

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

ssh与我的自定义shell一起工作,但不能直接使用ssh执行命令,shell,ssh,scp,Shell,Ssh,Scp,我搜索了整个网站,但没有找到类似的问题和解决方案。于是又问了一个新问题。 我正试图用一些有限的命令构建一个自定义shell。首先,我使用Stephen Brennan提供的。它有3个内置命令cd、help、exit,还可以执行系统命令 我在/etc/passwd中编辑了这一行: 并将此行添加到/etc/shell: 现在我可以使用ssh连接到远程主机,我的自定义shell出现了,我可以在其中输入命令;但是我不能直接使用ssh执行命令。例如,当我尝试使用ssh 192.168.32.1帮助在远程主

我搜索了整个网站,但没有找到类似的问题和解决方案。于是又问了一个新问题。 我正试图用一些有限的命令构建一个自定义shell。首先,我使用Stephen Brennan提供的。它有3个内置命令cd、help、exit,还可以执行系统命令

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

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

现在我可以使用ssh连接到远程主机,我的自定义shell出现了,我可以在其中输入命令;但是我不能直接使用ssh执行命令。例如,当我尝试使用ssh 192.168.32.1帮助在远程主机上运行help命令时,什么都没有发生。我尝试了ssh-v192.168.32.1帮助,结果如下。它停留在debug1:发送命令:help


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

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

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

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

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

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

当您的shell以这种方式从SSH服务器调用时,您应该发现argc和argv将反映命令行参数[/name/of shell,-c,the command]。有一些标准函数,您可以调用它们来帮助解析命令行参数


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

非常感谢您的详细解释。这正是我所缺少的。现在一切都正常了。不幸的是,我不能投票支持你的答案,因为我的声誉低于15。
/mnt/n1/custom-shell
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
debug1: Sending command: scp -v -t /mnt/n1/
$SHELL -c 'the-command'
int main(int argc, char **argv)
{
    ...