Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图从远程服务器执行PHP文件_Php_Json_Networking_Phpseclib_Ssh2 Exec - Fatal编程技术网

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

试图从远程服务器执行PHP文件,php,json,networking,phpseclib,ssh2-exec,Php,Json,Networking,Phpseclib,Ssh2 Exec,我试图从远程服务器执行一个php文件,就像在远程服务器控制台中键入“php example.php”一样。目前,它一直试图返回原始服务器。我尝试直接从php、Net_SSH2和执行a.sh执行。我不确定我会错在哪里。我会尽我所能把这个问题分解开来 数据库 ------------------------ | ID | password | ------------------------ | 50 | testpassword | ----------------------

我试图从远程服务器执行一个php文件,就像在远程服务器控制台中键入“php example.php”一样。目前,它一直试图返回原始服务器。我尝试直接从php、Net_SSH2和执行a.sh执行。我不确定我会错在哪里。我会尽我所能把这个问题分解开来

数据库

------------------------
|   ID | password      |
------------------------
|   50 | testpassword  |
------------------------
文件

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

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

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

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

?>
grabid.php

<?php
    $password="testingpassword";
    include('carrytoserver.php');
    include('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("echo $password > /test/example/carry.txt");
?>
<?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');
?>
<?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);

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

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

?>
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文件或继续运行。如果我在grabid.php顶部添加$password并从10.0.0.20控制台运行,它将运行

如果我加上 回音“嗨”; 到grabid.php的末尾,并从头开始运行,它将在10.0.0.10服务器控制台上回显“hi”

我还在execpw.php文件中尝试了其他一些方法

execpw2.php

<?php
    $password="testingpassword";
    include('carrytoserver.php');
    include('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("echo $password > /test/example/carry.txt");
?>
<?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');
?>
<?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);

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

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

?>

execpw3.php

<?php
    $password="testingpassword";
    include('carrytoserver.php');
    include('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("echo $password > /test/example/carry.txt");
?>
<?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');
?>
<?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);

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

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

?>

编辑:

如果carry.txt和grabid.php路径正确,则应如下更改grabid.php的行:

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

我可以通过将“文件内容”更改为绝对路径来修复它

<?php

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


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

?>


我假设它与权限有关。只是想澄清一下,当您自己从
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并通过makeconfig.php运行。如果我在“include makeconfig.php”之后放置一个echo,它将在10.0.0.10控制台中提供echo,但不会在10.0.0.20上生成配置文件。当您从shell运行php命令时,它将以您的身份运行(在您的示例中为-root),我相当肯定
serverusername
没有创建json输出文件的权限,但是我不能完全确定。我认为你的本地脚本应该说
php grabid.php
,而不是
echo“php grabid.php”
@neubert你是对的。这确实起了作用。我的主服务器输出“无法打开输入文件:grabid.php”。出于某种原因,它一直试图在主服务器上运行我返回给它的任何命令。就像我在grabid.php中回显“hi”一样,它将在主服务器控制台上返回。感谢您的帮助,当我将实际代码带到示例区域时,我犯了一个错误。我修好了,但不是问题。在应用服务器上,两个区域中都有carry.txt文件,两个区域中都有“testpassword”。以消除carrytoserver.php存在的问题,并消除文件路径不正确。我将编辑以上内容。谢谢,我在我的虚拟机中尝试了你的代码(数据库部分除外),唯一的问题是我前面说过的file_get_contents()函数中的文件路径。请尝试更改file_get_contents()中的文件路径,如上所示,给出文件的绝对路径。