paramiko 中的channel.recv_exit_status 始终返回通过invoke_shell 执行的第一个命令的返回代码

发布于 2024-12-16 21:58:20 字数 902 浏览 2 评论 0原文

我在下面的返回代码中发现一些差异。可能是我没有以正确的方式使用。每次我打印返回代码时,它都会打印与第一个返回代码相同的内容。我需要重置返回码吗?我的意思是在下面的示例中,我得到的两个命令的返回码均为 2。现在,当我交换这两个命令时,我的意思是用 ls -al file_not_exist;exit\n 替换 ls -al;exit\n ,反之亦然,它会打印返回代码 0每次它都会打印与第一个返回相同的返回代码。

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
ssh.connect('localhost', username='sam', password='mypassword')

channel = ssh.invoke_shell()
channel.send('ls -al file_not_exist;exit\n') #Sending to list a file which doesn't exist
time.sleep(3)
print("My 1st command exit status is: ", channel.exit_status_ready())
print("My 1st command return code is: ", channel.recv_exit_status())

channel.send('ls -al;exit\n')
time.sleep(3)
print("My 2nd command exit status is: ",channel.exit_status_ready())
print("My 2nd command return code is: ",channel.recv_exit_status())

我需要打印每个命令的返回码。您能帮我解决这个问题吗?

I am getting some discrepancy in the return code below. May be I am not using in the proper way. Every-time I print the return code, it prints the same as the first one has returned. Do I need to reset the return code. I mean in the following example, I am getting the return code as 2 for both the commands. Now when I interchange both the commands, I mean replace ls -al;exit\n with ls -al file_not_exist;exit\n and vice versa, it prints return code 0. Each time it prints the same return code as the first one has returned.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
ssh.connect('localhost', username='sam', password='mypassword')

channel = ssh.invoke_shell()
channel.send('ls -al file_not_exist;exit\n') #Sending to list a file which doesn't exist
time.sleep(3)
print("My 1st command exit status is: ", channel.exit_status_ready())
print("My 1st command return code is: ", channel.recv_exit_status())

channel.send('ls -al;exit\n')
time.sleep(3)
print("My 2nd command exit status is: ",channel.exit_status_ready())
print("My 2nd command return code is: ",channel.recv_exit_status())

I need to print the return code of each command. Could you please help me in how to get this issue resolved ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2024-12-23 21:58:20

通过 ssh 发回的退出状态是 ssh 进程执行的远程命令的退出状态。您只能获得一种退出状态,在 invoke_shell() 的情况下,即 shell 本身的退出状态。在该 shell 中运行的命令的退出状态仅在该环境中已知。

您要么需要检索并解析 shell 或脚本中的状态(bash 快捷方式是变量 $?),要么使用 exec_command() 独立执行命令。

参见:
我可以吗获取通过 ssh 在子 shell 中执行的命令的退出代码?

The exit status sent back over ssh is that of the remote command execute by the ssh process. You only get one exit status, and in the case of invoke_shell(), that is the exit status of the shell itself. The exit status of commands run within that shell are known only in that environment.

You either need to retrieve and parse the status in the shell or script (the bash shortcut is the variable $?), or execute your commands independently with exec_command().

See also:
Can I get the exit code of a command executed in a subshell via ssh?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文