我可以通过 ssh 获取在子 shell 中执行的命令的退出代码吗? [英] Can I get the exit code of a command executed in a subshell via ssh?

查看:74
本文介绍了我可以通过 ssh 获取在子 shell 中执行的命令的退出代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Paramiko 编写部署脚本,但我在运行命令时遇到退出代码问题.我使用的代码类似于 这个答案,但它有点复杂.基本上,从我们的开发箱我们必须经过一个跳转服务器,然后从那里到一系列生产机器.在那里我们必须切换到系统用户(sudo su - systemuser),然后我们可以运行命令.

I'm trying to use Paramiko to write a deployment script, and I'm having trouble with exit codes from the commands I run. I'm using code similar to that in this answer, but it's a little more complicated. Basically, from our dev boxes we have to go through a jump server, and from there to a series of production machines. Once there we have to switch to a system user (sudo su - systemuser) and then we can run commands.

问题是,据我所知,我有 3 个子外壳 - ssh 会话、嵌套的 ssh 命令和 su 子外壳.我无法让 Paramiko 将内部子shell 中的命令的退出代码返回给我 - 我猜它最终将返回的退出代码将是 ssh 命令的退出代码.我怀疑这个问题实际上并不是 Paramiko 特有的 - SSH 协议甚至支持这种用法吗?

The problem is that as I understand it I have 3 subshells - the ssh session, the nested ssh command and then the su subshell. I can't get Paramiko to give me back the exit code of the commands in the inner subshell - I guess the exit code it will eventually return will be that of the ssh command. I suspect this problem is not actually specific to Paramiko - does the SSH protocol even support this kind of usage?

我目前一直在执行:

(my command); echo "Process terminated with exit code $?"

然后在客户端解析出来,但它很丑 - 有没有更好的方法?

and then parsing this out on the client, but it's pretty ugly - is there a nicer way?

推荐答案

我猜它最终会返回的退出代码将是 ssh 命令的退出代码.我怀疑这个问题实际上并不是 Paramiko 特有的 - SSH 协议甚至支持这种用法吗?

I guess the exit code it will eventually return will be that of the ssh command. I suspect this problem is not actually specific to Paramiko - does the SSH protocol even support this kind of usage?

是的,也不是.

总是返回退出状态,但可能不是你想的那样.一旦你调用 invoke_shell(),你的远程进程就是一个 shell,可能是 bash.您收到的 exit_status 将是来自该 shell 的那个,不是它运行的任何进程.Paramiko(也不是任何 ssh 实现)永远无法返回命令的退出状态,因为在该远程 shell 之外永远不会看到退出状态.由您决定退出状态应该是什么,并使用 exit $EXIT_STATUS 退出 shell(通常 exit $? 就足够了)

The exit status is always returned, but it may not be from where you think. Once you call invoke_shell(), your remote process is a shell, probably bash. The exit_status you receive will be the one from that shell, not any of the processes it ran. Paramiko (nor any ssh implementation) can ever return the exit status of your commands, because the exit statuses are never seen outside of that remote shell. It's up to you to determine what your exit status should be, and exit the shell with exit $EXIT_STATUS (often exit $? is enough)

如果你可以分解你的命令,或者将它们包装在一个脚本中,然后使用 exec_command() 方法,你将可以直接访问正确的退出状态.

If you can break up your commands, or wrap them in a script, then use the exec_command() method, you will have access to the correct exit status directly.

或者,您可以在远程shell中使用shell命令exec,shell的进程将被exec调用的命令替换,并返回其退出状态.

Alternatively, you can use shell command exec in the remote shell, and the shell's process will be replaced by the command called by exec, and its exit status will be returned.

所有这三种方法都会按预期设置 channel.exit_status 属性.

All three of these methods will set the channel.exit_status attribute as expected.

这篇关于我可以通过 ssh 获取在子 shell 中执行的命令的退出代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆