1

i try to update my minecraft whitelist over php to my linux server. The connection works but i dont know why he didnt send my command to add the user to the whitelist.

Here my code:

$name=$_POST['name'];
$mc=$_POST['mc'];
if($ssh = ssh2_connect('127.0.0.1', 22)) {
if(ssh2_auth_password($ssh, 'root', 'password')) {
    $stream = ssh2_exec($ssh, 'screen -R '.$mc.' && '.'whitelist add '.$name.' && whitelist reload');
    stream_set_blocking($stream, true);
    $data = '';
    while($buffer = fread($stream, 4096)) {
        $data .= $buffer;
    }
    fclose($stream);
    echo $data;
}
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Baldrian
  • 11
  • 1
  • Sending unfiltered POST data to the shell is never going to end well. – rjdown May 28 '16 at 15:36
  • You want to execute command `whitelist add xxx && whitelist reload` inside screen? – Andrew May 28 '16 at 15:50
  • yes first i want to open the screen with screen -R mctechnic than i wand to add userxyz to whitelist oh 2answers didnt see xD first time @stackoverflow i tried to do it without POST screen -R mctechnic didnt work either – Baldrian May 28 '16 at 16:12

1 Answers1

1

You are joining commands with && and all of them will be executed one after another in your current shell, not inside screen. screen can execute commands inside selected session, but with it's own syntax. See this question, for example, or manual. In your case, command must look like this:

screen -S session_name -X stuff $'whitelist add user_name && whitelist reload\n'

Note \n symbol at the end of string. It is required in order to execute command because screen will not really execute it, screen will "send the specified command to a running screen session" as manual says. Consider that screen will type command instead of you. And when you typing command, you must press Enter/Return to execute it.

In PHP you can execute command this way:

$stream = ssh2_exec($ssh, "screen -S $mc -X stuff \$'whitelist add $name && whitelist reload\\n'");
Andrew
  • 1,858
  • 13
  • 15