Trying to Execute PHP File From Remote Server

3.4k Views Asked by At

I am trying to execute a php file from a remote server as if I am typing "php example.php" into the remote servers console. Currently it keeps trying to give back to the original server. I have tried to execute straight from php, Net_SSH2, and executing a .sh. I am not sure where I am going wrong. I will try to break down the issue the best I can.

Database

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

Files

Main Server (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

All permissions are 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 will execute carrytoserver.php which will carry a password over to 10.0.0.20 and place it into carry.txt. It will then execute execpw.php which will execute grabid.php on 10.0.0.20. grabid.php will then grab the ID related to the password.

Once it does all of this 10.0.0.20 should have $password and $id. Grabid.php will then execute makeconfig.php and it will create the json config file for the application.

Currently it will carry the password over to 10.0.0.20 and will execute grabid.php but will not create the json file or continue running. If I add $password to the top of grabid.php and run in from 10.0.0.20 console it will run.

If I add echo "hi"; to the end of grabid.php and run from the beginning it will echo "hi" onto 10.0.0.10 server console.

I have also tried a few other things in the execpw.php file

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 [email protected] 'php grabid.php'";
exec ($executephp);

?>

All give the same result. I am sorry for the overload of information but I am trying to provide as much information as I can.

2

There are 2 best solutions below

2
On

EDIT :

If carry.txt and grabid.php paths are correct, then you should change the line of grabid.php as follows:

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

I was able to fix it by changing the "file_put_contents" to an absolute path.

<?php

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


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

?>