gpt4 book ai didi

python - 如何通过 Paramiko 获取广播消息

转载 作者:行者123 更新时间:2023-12-02 14:27:25 26 4
gpt4 key购买 nike

我正在尝试通过 paramiko 获取广播消息,以确保操作已完全执行。像这样的东西。

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, 22, user, passwd, timeout=180)
command = 'sleep 3; wall "The system will shutdown in 3 seconds."; sudo nohup shutdown -h now;'
stdin, stdout, stderr = ssh.exec_command(command, timeout=300)
if "The system will shutdown in 3 seconds." in stdout.read().encode('utf-8'):
print "Command success!!"
else
print "Command failure!!"

但是,我总是得到一个空输出。我怎样才能得到输出?

最佳答案

广播消息仅发送到交互式 session 。

所以你至少需要设置get_pty SSHClient.exec_command的参数至True .

也许你甚至不得不通过 SSHClient.invoke_shell 使用 shell channel 。 .

虽然实际上,我不明白你为什么要捕获广播。这“确保”了什么?也许像检查 wall退出代码可能是一种更可靠的方法。

关于python - 如何通过 Paramiko 获取广播消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61609686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com