0

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 root@10.0.0.20 '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.

neubert
  • 15,947
  • 24
  • 120
  • 212
benswmay
  • 1
  • 1
  • 4
  • I'm going to assume it has something to do with permissions. Just to clarify, when you run the `grabid.php` file yourself from `10.0.0.20` it works? But when you run the whole script and let the SSH client run `grabid.php` from `10.0.0.10` it doesn't work? Are you logging into 10.0.0.20 as `serverusername`? – Scopey Dec 04 '14 at 01:35
  • I was thinking the same so I opened the permissions on the file, I'm sure there is more I could do though, and yes I just kind of made it as generic as possible but for all access I am currently using root. I will change above. And yes when I run it on 10.0.0.20 it works but when I run it on 10.0.0.10 it runs up to and through makeconfig.php. If I place an echo after the "include makeconfig.php" it will give echo in the 10.0.0.10 console but not make the config file on 10.0.0.20. – benswmay Dec 04 '14 at 03:15
  • When you run a php command from a shell it will run as you (in your case - root), I am fairly sure that it will be that `serverusername` does not have permission to create the json output file, but I can't be completely sure. – Scopey Dec 04 '14 at 03:42
  • I think your local_script should say `php grabid.php` instead of `echo "php grabid.php"` – neubert Dec 04 '14 at 22:15
  • @neubert you were right. That did actually kind of work. My main server output "Could not open input file: grabid.php". For some reason it keeps trying to run whatever command I give it back on the main server. Like if I echo "hi" in my grabid.php it will return on the main server console. Thanks for the help – benswmay Dec 09 '14 at 02:09

2 Answers2

1

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');
chipaki
  • 52
  • 1
  • 4
  • I made a mistake when I brought my actual code to my example area. I fixed it but it wasn't the problem. On the application server I also have the carry.txt file in both areas with "testpassword" in both. To eliminate there being an issue with carrytoserver.php and also to eliminate the file path being incorrect. I will edit above. Thanks – benswmay Dec 04 '14 at 12:42
  • I try your code (except database part) in my virtual machines and the only problem was the file path in file_get_contents() function as I previously said. Please try to change the file path in file_get_contents() as I show above giving the _absolute path of the file_. – chipaki Dec 04 '14 at 21:32
0

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

?>
E_net4
  • 27,810
  • 13
  • 101
  • 139
benswmay
  • 1
  • 1
  • 4