shell - ssh 可与我的自定义 shell 配合使用,但无法直接使用 ssh 执行命令

标签 shell ssh scp

我在网站上搜索过,但没有找到类似的问题和解决方案。于是又问了一个新问题。 我正在尝试使用一些有限的命令构建自定义 shell 。作为起点,我使用 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
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f2efeff4c0b1b9b2aeb1b6b8aeb3b2aeb1" rel="noreferrer noopener nofollow">[email protected]</a>'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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0f1212093d4c444f534c4b45534e4f534c" rel="noreferrer noopener nofollow">[email protected]</a>:/mnt/n1/ 将文件复制到远程主机它停留在这一行:

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

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

最佳答案

调用 shell 并让它运行命令的标准方法之一是使用命令行参数 -c 和要运行的命令来调用 shell 程序。这是 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 将反射(reflect)命令行参数 ["/name/of-shell", "-c", "the-command"]。有像 getopt(3) 这样的标准函数您可以调用它来帮助解析命令行参数。

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

关于shell - ssh 可与我的自定义 shell 配合使用,但无法直接使用 ssh 执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52604245/

相关文章:

bash - 在 bash 中按特定顺序打印文件内容

bash - shell脚本测试

java - JSch异常: No Configuration was registered named com. sun.security.jgss.krb5.initiate

mysql - 远程mysql导入: No such file or directory

python - Paramiko scp 从远程机器正则表达式复制

linux - 本地到远程复制的scp递归命令

c# - SharpSSH 的 Scp 对象无法复制大于 2GB 的文件

linux - 比较两个文件中的字段并在 shell 中打印到另一个文件

使用 GIT_SSH 错误使用自定义 SSH 进行 Git 克隆

linux - 如何监视文件并在终端中打印出更改(使用 inotify)?