简体   繁体   English

在python脚本中通过ssh执行哪个命令

[英]execute which command over ssh in python script

I'm trying to run the command which solsql over SSH in a Python script.我正在尝试在 Python 脚本中通过 SSH 运行which solsql命令。

I think the problem is in the ssh command and not the Python part, but maybe it's both.我认为问题出在ssh命令而不是 Python 部分,但也许两者都有。

I tried我试过

subprocess.check_output("ssh root@IP which solsql",
    stderr=subprocess.STDOUT, shell=True)

but I get an error.但我收到一个错误。

I tried to run the command manually:我尝试手动运行命令:

ssh root@{server_IP}" which solsql"

and I get a different output.我得到了不同的输出。

On the server I get the real path ( /opt/solidDB/soliddb-6.5/bin/solsql ) but over SSH I get this:在服务器上,我得到了真实路径( /opt/solidDB/soliddb-6.5/bin/solsql ),但通过 SSH 我得到了这个:

which: no solsql in
(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin)

I think what your looking for is something like paramiko .我认为您要寻找的是paramiko 之类的东西 An example of how to use the library and issue a command to the remote system.如何使用库并向远程系统发出命令的示例。

import base64
import paramiko
key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('ssh.example.com', username='THE_USER', password='THE_PASSWORD')
stdin, stdout, stderr = client.exec_command('which solsql')
for line in stdout:
    print('... ' + line.strip('\n'))
client.close()

When you run a command over SSH, your shell executes a different set of startup files than when you connect interactively to the server.当您通过 SSH 运行命令时,您的 shell 会执行一组与您交互连接到服务器时不同的启动文件。 So the fundamental problem is really that the path where this tool is installed is not in your PATH when you connect via ssh from a script.因此,根本问题实际上是当您从脚本通过ssh连接时,安装此工具的路径不在您的PATH

A common but crude workaround is to force the shell to read in the file with the PATH definition you want;一个常见但粗略的解决方法是强制 shell 读入具有您想要的PATH定义的文件; but of course that basically requires you to know at least where the correct PATH is set, so you might as well just figure out where exactly the tool is installed in the first place anyway.但当然,这基本上要求您至少知道正确的PATH设置在哪里,因此您不妨首先弄清楚该工具的确切安装位置。

ssh server '. .bashrc; type -all solsql'

(assuming that the PATH is set up in your .bashrc ; and ignoring for the time being the difference between executing stuff as yourself and as root . The dot and space before .bashrc are quite significant. Notice also how we use the POSIX command type rather than the brittle which command which should have died a natural but horrible death decades ago). (假设PATH在您的.bashrc设置;并且暂时忽略以自己和root身份执行内容之间的区别.bashrc之前的点和空格非常重要。还要注意我们如何使用 POSIX 命令type而不是脆弱的which命令应该在几十年前自然但可怕的死亡)。

If you have a good idea of where the tool might be installed, perhaps instead do如果您很清楚该工具的安装位置,也许可以这样做

subprocess.check_output(['ssh', 'root@' + ip, '''
    for path in /opt/solidDB/*/bin /usr/local/bin /usr/bin; do
         test -x "$path/solsql" || continue
         echo "$path"
         exit 0
     done
     exit 1'''])

Notice how we also avoid the (here, useless) shell=True .注意我们如何避免(这里是无用的) shell=True Perhaps see also Actual meaning of 'shell=True' in subprocess也许还可以参见子进程中“shell=True”的实际含义

First, you need to debug your error.首先,您需要调试错误。

Use the code like this:使用这样的代码:

command = "ssh root@IP which solsql"

try:
    retult = subprocess.check_output(command,shell=True,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

print ("Result:", result)

It will output error message to you, and you'll know what to do, for example, ssh could have asked for a password, or didn't find your key, or something else.它会向您输出错误消息,您将知道该怎么做,例如,ssh 可能要求输入密码,或者没有找到您的密钥,或其他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM