尝试从远程服务器执行PHP文件

时间:2014-12-04 01:19:17

标签: php json networking phpseclib ssh2-exec

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

App Server(10.0.0.20):grabid.php,login.php,makeconfig.php,local_script.php,carry.txt

所有权限均为777

MAINSERVER

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');
?>

APPSERVER

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';

?>

makeconfig.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。然后,grabid.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控制台运行它将运行。

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

我还在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);

?>

所有人给出相同的结果。我很抱歉信息过载,但我想提供尽可能多的信息。

2 个答案:

答案 0 :(得分:1)

编辑:

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

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

答案 1 :(得分: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);

?>

感谢您的帮助!