【问题标题】:Execute a command after successfully SSH connectionSSH连接成功后执行命令
【发布时间】:2014-09-02 12:35:15
【问题描述】:

我想我可能在谷歌上搜索这些东西是错误的,但是我想知道我是否可以让我的树莓派在通过 SSH 连接到它后执行命令。

工作流程: 1)通过终端SSH进入Pi 2)登录后,树莓派执行命令显示当前温度(我已经知道这个命令)

pi 已经输出了

Linux raspberrypi 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri Jul 11 15:11:35 2014

我可能完全误解了这一切,甚至可能在上面的对话框中执行并显示了命令。

【问题讨论】:

  • 这取决于外壳的类型,但您可以向用户添加命令 .bashrc、.profile(或您的外壳使用的任何内容)。

标签: python linux ssh terminal raspberry-pi


【解决方案1】:

您可以通过 bash 命令执行命令,而无需实际登录另一台计算机,只需将命令放在 shh 命令之后:

$ ssh pi@pi_addr touch wat.txt

将创建文本文件 ~/wat.txt。

这对于自动化来说有点麻烦,但是因为必须提供密码,因此您可以在计算机上设置公共/私有 RSA 密钥,以便能够在没有密码的情况下远程登录您的 pi。只需执行以下操作:

$ ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/a/.ssh/id_rsa):
    Created directory '/home/a/.ssh'.
    Enter passphase (empty for no passphrase):
    Enter the same passphrase again:
$ssh pi@pi_addr mkdir -p .ssh
$cat .ssh/id_rsa.pub | ssh pi@pi_addr 'cat >> .ssh/authorized_keys'

在运行 ssh-keygen 时不要输入密码并保持一切默认。现在您无需输入密码即可运行 ssh pi@pi_addr。

示例python文件:

import subprocess

SERVER = "pi@pi_addr"
subprocess.call("ssh pi@pi_addr touch wat.txt")

【讨论】:

    【解决方案2】:

    您正在寻找的是在各种 linux 发行版中很常见的 motd。这不在python中(但可以)。 motd 在通过 SSH 登录时运行多个命令,并构造一条输出给用户的消息。更多信息(实际上列出了温度)可以在这里找到:Rapberry Pi Welcome Message。问题是这可能会根据 linux 发行版略有变化。一个很好的 git repo 也可以在这里找到一个很好的消息:Raspberry Pi Motd

    【讨论】:

      【解决方案3】:

      只需将命令附加到 ssh 命令即可。

      ssh user@server "echo test"

      "echo test" 在远程机器上执行。

      【讨论】:

        【解决方案4】:

        是的,这可以做到。我知道的方法使用subprocesshttps://docs.python.org/2/library/subprocess.html。只要您知道 python 脚本的名称和参数(如果有),您就可以将它们传递给subprocess。以下是如何通过 python ssh 脚本连接的示例(取自http://python-for-system-administrators.readthedocs.org/en/latest/ssh.html):

            import subprocess
            import sys
        
            HOST="www.example.org"
            # Ports are handled in ~/.ssh/config since we use OpenSSH
            COMMAND="uname -a"
        
            ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                           shell=False,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
            result = ssh.stdout.readlines()
            if result == []:
                error = ssh.stderr.readlines()
                print >>sys.stderr, "ERROR: %s" % error
            else:
                print result
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-23
          • 1970-01-01
          • 2021-12-02
          • 1970-01-01
          • 2023-04-08
          • 2021-06-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多