【问题标题】:Trying to Execute PHP File From Remote Server尝试从远程服务器执行 PHP 文件
【发布时间】:2014-12-04 01:19:17
【问题描述】:

我正在尝试从远程服务器执行 php 文件,就好像我在远程服务器控制台中键入“php example.php”一样。目前它一直在尝试回馈原始服务器。我尝试直接从 php、Net_SSH2 执行并执行 .sh。我不确定我哪里出错了。我会尽力解决这个问题。

数据库

------------------------
|   ID | password      |
------------------------
|   50 | testpassword  |
------------------------

文件

主服务器(10.0.0.10): carrytoserver.php、execpw.php、execpw2.php、execpw3.php、execute.php

应用服务器(10.0.0.20):grapid.php、login.php、makeconfig.php、local_script.php、carry.txt

所有权限都是777

主服务器

execute.php

<?php
    $password="testingpassword";
    include('carrytoserver.php');
    include('execpw.php');
?>

carrytoserver.php

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


    $ssh = new Net_SSH2('10.0.0.20');
    if (!$ssh->login('root', 'serverpassword')) {
    exit('Login Failed');
}


echo $ssh->exec("echo $password > /test/example/carry.txt");
?>

execpw.php

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

    $ssh = new Net_SSH2('10.0.0.20');
    if (!$ssh->login('root', 'serverpassword')) {
        exit('Login Failed');
    }

    echo $ssh->exec('php /test/example/grabid.php');
?>

应用服务器

carry.txt

testpassword

grabid.php

<?php 
include 'login.php';
$carrypw=file_get_contents('carry.txt');
$password=str_replace("\n","", $carrypw);
$con=mysql_connect("$host", "$username", "$dbpassword")or die("cannot connect"); 
mysql_select_db("$db_name") or die ("cannot select DB"); 

  $result = mysql_query("SELECT * FROM example
WHERE password='$password'");

while($row = mysql_fetch_array($result)) {
  $id = $row['ID'];

}

include 'makeconfig.php';

?>

ma​​keconfig.php

<?php

$return_arr = array('ID' => $id, 'password' => "$password");


$json_data = json_encode($return_arr);
file_put_contents("config.json", $json_data);

?>

local_script

#! /bin/bash
echo "php grabid.php"

execute.php 将执行 carrytoserver.php,它会将密码传送到 10.0.0.20 并将其放入 carry.txt。然后它将执行 execpw.php,它将在 10.0.0.20 上执行 grabid.php。然后grapid.php会抓取与密码相关的ID。

一旦它完成所有这 10.0.0.20 应该有 $password 和 $id。 Grabid.php 将执行 makeconfig.php 并为应用程序创建 json 配置文件。

目前它将携带密码到 10.0.0.20 并执行 grabid.php 但不会创建 json 文件或继续运行。如果我将 $password 添加到 grabid.php 的顶部并从 10.0.0.20 控制台运行,它将运行。

如果我添加 回声“嗨”; 到grapid.php 的末尾并从头开始运行它会在10.0.0.10 服务器控制台上回显“hi”。

我还在 execpw.php 文件中尝试了一些其他的东西

execpw2.php

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

$ssh = new Net_SSH2('10.0.0.20');
if (!$ssh->login('root', 'serverpassword')) {
    exit('Login Failed');
}

echo $ssh->exec('/test/example/local_script.sh');
?>

execpw3.php

<?php

$executephp="ssh root@10.0.0.20 'php grabid.php'";
exec ($executephp);

?>

所有都给出相同的结果。对于信息过多,我深表歉意,但我会尽力提供尽可能多的信息。

【问题讨论】:

  • 我假设它与权限有关。澄清一下,当您自己从10.0.0.20 运行grabid.php 文件时,它可以工作吗?但是,当您运行整个脚本并让 SSH 客户端从 10.0.0.10 运行 grabid.php 时,它不起作用吗?您是否以 serverusername 身份登录 10.0.0.20?
  • 我也是这么想的,所以我打开了文件的权限,但我确信我可以做的更多,是的,我只是让它尽可能通用,但适用于所有访问我目前正在使用root。我会在上面改变。是的,当我在 10.0.0.20 上运行它时,它可以工作,但是当我在 10.0.0.10 上运行它时,它会运行到并通过 makeconfig.php。如果我在“include makeconfig.php”之后放置一个回显,它将在 10.0.0.10 控制台中给出回显,但不会在 10.0.0.20 上创建配置文件。
  • 当您从 shell 运行 php 命令时,它将以您的身份运行(在您的情况下 - 根),我相当确定 serverusername 没有创建 json 的权限输出文件,但我不能完全确定。
  • 我认为你的 local_script 应该说 php grabid.php 而不是 echo "php grabid.php"
  • @neubert 你是对的。这确实起到了一定的作用。我的主服务器输出“无法打开输入文件:grapid.php”。出于某种原因,它一直在尝试运行我在主服务器上返回的任何命令。就像我在我的grapid.php 中回显“hi”一样,它将在主服务器控制台上返回。感谢您的帮助

标签: php json networking phpseclib ssh2-exec


【解决方案1】:

编辑:

如果carry.txt和grapid.php路径正确,那么你应该把grapid.php的那一行改成如下:

$carrypw=file_get_contents('/test/example/carry.txt');

【讨论】:

  • 当我把我的实际代码带到我的示例区域时,我犯了一个错误。我修好了,但这不是问题。在应用程序服务器上,我在两个区域中也都有带有“testpassword”的 carry.txt 文件。消除carrytoserver.php 的问题并消除文件路径不正确。我将在上面进行编辑。谢谢
  • 我在我的虚拟机中尝试了您的代码(数据库部分除外),唯一的问题是我之前所说的 file_get_contents() 函数中的文件路径。请尝试更改 file_get_contents() 中的文件路径,如上所示,给出文件的绝对路径
【解决方案2】:

我能够通过将“file_put_contents”更改为绝对路径来修复它。

<?php

$return_arr = array('ID' => $id, 'password' => "$password");


$json_data = json_encode($return_arr);
file_put_contents("/test/example/config.json", $json_data);

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2020-09-12
    • 2011-06-24
    相关资源
    最近更新 更多