如何设置一个通过 ssh 接受输入的服务,使用输入运行 python 脚本,然后通过 ssh 返回输出?

如何设置一个通过 ssh 接受输入的服务,使用输入运行 python 脚本,然后通过 ssh 返回输出?

我有一台运行 ubuntu 的服务器,并具有运行大型深度学习模型的 GPU。我希望这台服务器首先接受包含通过 SSH 输入的请求,然后将该输入提供给 DL 模型以获取输出(顺便说一句,通过 python 脚本运行模型),最后通过 SSH 将输出返回到原始版本发件人。我该怎么办呢?我想过建立一项服务,但也许有更好的方法可以做到这一点。欢迎任何建议。谢谢。

答案1

man sshd;部分AUTHORIZED_KEYS FILE FORMAT;选项command="command"

这是一个例子:

$ head ~/.ssh/authorized_keys
command="cat > /home/vagrant/ssh-output" ssh-rsa AAAAB3NzaC1yc...
$ ls ssh-output
ls: cannot access 'ssh-output': No such file or directory
$ echo 'blah blah blah' | ssh -i .ssh/id_rsa localhost 
Pseudo-terminal will not be allocated because stdin is not a terminal.
$ cat ssh-output 
blah blah blah

观察没有生成贝壳。相反,在客户端通过管道输入的内容ssh被传递到 ssh 服务器端的 cat 的标准输入。对于您的场景,您可以替换command=""为 python 脚本的路径。

请注意,您必须知道用户和用户的 pub_key,然后才能为他们配置此功能;这不是通用配置。现在,如果您发布用户名和私钥,那么您可以告诉您的用户使用它们,并将其作为通用服务提供,但共享私钥(尤其是发布/常用的密钥)很容易被滥用。

另请注意,此配置将允许客户端打开到该计算机的端口转发或 X11 转发。这可以受到限制;我将向您指出相关的手册页。

相关内容