Linux PHP shell_exec ssh 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7744793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:39:27  来源:igfitidea点击:

PHP shell_exec ssh connection

phplinuxsshshell-exec

提问by bikerben

I know this question has been asked before in many different ways but I'm still scratching my head over why I can't get this to work.

我知道这个问题以前曾以多种不同的方式被问过,但我仍然在为为什么我不能让它发挥作用而挠头。

Firstly I have two SLES servers setup, these are Server A & Server B which are both running on a small private network which is only accessed by a dedicated team.

首先,我设置了两个 SLES 服务器,它们是服务器 A 和服务器 B,它们都运行在一个只能由专门团队访问的小型专用网络上。

Server A is configured as a web server which is running Apache, PHP, MYSQL and ssh all of which are running problem free.

服务器 A 配置为运行 Apache、PHP、MYSQL 和 ssh 的 Web 服务器,所有这些都运行无问题。

Server B is used to run menial tasks with ssh also installed and activated.

服务器 B 用于在安装和激活 ssh 的情况下运行琐碎的任务。

I have created my rsa key on Server A and installed it on Server B which when run at the command line logs me in straight away with out asking for a password. I have repeated this process for both root & nobody accounts on Server A.

我已经在服务器 A 上创建了我的 rsa 密钥并将其安装在服务器 B 上,当在命令行运行时,它会立即让我登录,而无需输入密码。我已经为服务器 A 上的 root 和 nobody 帐户重复了此过程。

I have added this a PHP page to Server A which looks like:

我已将此 PHP 页面添加到服务器 A,如下所示:

<?php
shell_exec('ssh [email protected] ./StartTest.sh');

header("Location: archive.php?page=home"); 
?>

But when I run it it does not create my folder. If I run this from the command line it works for both (I think both, I can't recall if I did try this for the nobody account on the cli now) root & the nobody account. I even went as far as adding the nobody account to the root group but still no joy.

但是当我运行它时,它不会创建我的文件夹。如果我从命令行运行它,它对两者都有效(我认为两者都适用,我不记得我现在是否为 cli 上的 nobody 帐户尝试过此操作)root 和 nobody 帐户。我什至将nobody 帐户添加到root 组,但仍然没有任何乐趣。

Have I missed some thing here. All I would like to do is connect from Server A to Server B via php & ssh to execute one command and redirect to a another page on the web site.

我在这里错过了什么。我想要做的就是通过 php 和 ssh 从服务器 A 连接到服务器 B,以执行一个命令并重定向到网站上的另一个页面。

Any help would be graciously appreciated as my paracetamol stock is running low.

由于我的扑热息痛库存不足,任何帮助将不胜感激。

采纳答案by nigol

The built-in SSH support George Cummins speaks of is non-existent. It is an extension to PHP that's not included by default. It has to be compiled separately and is notoriously difficult to setup / use. My recommendation would be to use phpseclib, a pure PHP SSH implementation:

George Cummins 所说的内置 SSH 支持是不存在的。它是默认情况下不包含的 PHP 扩展。它必须单独编译,并且众所周知难以设置/使用。我的建议是使用phpseclib,一个纯 PHP SSH 实现

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

回答by George Cummins

You said "I have added this a PHP page", so I will assume that you are executing this script via your web server, rather than as a standalone script.

您说“我已经添加了一个 PHP 页面”,因此我假设您是通过 Web 服务器执行此脚本,而不是作为独立脚本执行。

As such, the script may not be running from the directory you expect. You should use absolute (rather than relative) paths to ensure that the script finds the sshbinary and your script:

因此,脚本可能不会从您期望的目录中运行。您应该使用绝对(而不是相对)路径来确保脚本找到ssh二进制文件和您的脚本:

shell_exec('/path/to/ssh [email protected] /home/yourdirectory/scripts/StartTest.sh');

You will also need to confirm that the webserver user had permissions to execute ssh and the StartTest.sh script.

您还需要确认网络服务器用户有权执行 ssh 和 StartTest.sh 脚本。

回答by onalbi

I know that I'm too late at this answer but maybe can help someone:

我知道我在这个答案上为时已晚,但也许可以帮助某人:

To use shell_execand sshyou need to add as parameter to sshthese

要使用shell_exec并且ssh您需要将其作为参数添加到ssh这些

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet

So the command doesn't try to create .ssh folder and you have a clear output without log of ssh

因此,该命令不会尝试创建 .ssh 文件夹,并且您可以在没有日志的情况下获得清晰的输出 ssh