0

我正在尝试从远程服务器执行 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

主服务器

执行.php

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

携带服务器.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");
?>

执行pw.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');
?>

应用服务器

携带.txt

testpassword

抢夺.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';

?>

生成配置文件

<?php

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


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

?>

本地脚本

#! /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 控制台运行,它将运行。

如果我添加 echo "hi"; 到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);

?>

都给出相同的结果。我很抱歉信息过多,但我正在努力提供尽可能多的信息。

4

2 回答 2

1

编辑 :

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

$carrypw=file_get_contents('/test/example/carry.txt');
于 2014-12-04T01:54:34.203 回答
0

我能够通过将“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);

?>
于 2014-12-11T00:49:56.837 回答