python - 使用 Paramiko 在远程机器上执行命令无效

标签 python ubuntu ssh ros paramiko

我正在使用一个使用 localhost 连接到远程 pc 的机器人。我使用 Paramiko 库将命令从远程 pc 发送到我的机器人。我想在/activate 主题上发布一个数字,如下所示:

rostopic pub /activate std_msgs/Int16 "data: 1"
问题不在于我的连接,因为上述命令使用 PuTTY 或 SSH 工作。
我写了以下代码:
import paramiko
host = "xxx.xxx.x.x"
port = 22
username = "robot"
password = "xxx"
command = "rostopic pub /activate std_msgs/Int16 'data: 1'"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)
stdin, stdout, stderr = ssh.exec_command(command)
但是使用此代码,我在我的机器人中看不到任何结果。任何人都可以帮忙吗?

最佳答案

如果您在执行命令时遇到问题,您需要阅读它们的(错误)输出以查看任何错误消息。

  • 此外,您当前的代码甚至没有完成命令。阅读命令输出实际上是 Paramiko 中真正完成命令的最简单方法。
    Wait to finish command executed with Python Paramiko
  • 有关另一个常见问题,请参阅 Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command
  • 关于python - 使用 Paramiko 在远程机器上执行命令无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68083651/

    相关文章:

    python - 根据 Pandas 中列的值删除行

    python - 我可以手动 ssh,但不能通过脚本 - 权限被拒绝(公钥)

    python - 使用 gdata python 客户端在博客上批量发布

    python - 从 OS X ssh 时出现 UnicodeDecodeError

    python - 如何在 Django 模型中存储列表

    docker - 问题 Docker 上没有这样的文件或目录

    ubuntu - 重启 NGINX 失败

    Python,如何正确地将一个函数(特别是文件)的结果传递给另一个函数?

    git - 从 SSH 克隆 git 存储库时,*.git 扩展名有什么用?

    ubuntu - 在 Ubuntu 上的/var 目录中使用 ssh key