-1

I need some help with escaping the quotes in this script, I have code above and below, but it does not pertain to this because if I just issue a screen -d -m -S minecraft, it creates one fine. Any help would be amazing because i've been trying for an hour now, Thanks!

 if (!($stream = ssh2_exec($con, 'screen -d -m -S minecraft -p 0 -X stuff "\printf "\say This is a test.\r"\"\' ))) {
        echo "fail: unable to execute command\n";
    } else {
        // collect returning data from command
        stream_set_blocking($stream, true);
        $data = "";
        while ($buf = fread($stream,4096)) {
            $data .= $buf;
        }
        echo $data;
        fclose($stream);
    }
Jeremy Sayers
  • 637
  • 4
  • 10
  • 23

2 Answers2

1

Shouldn't the \ should go before the ". Also, from your description, it sounds like that you only want to run "screen -d -m -S minecraft" and it looks like you have an extra printf in there.

if (!($stream = ssh2_exec($con, "screen -d -m -S minecraft")))

http://php.net/manual/en/language.types.string.php

David Z.
  • 5,621
  • 2
  • 20
  • 13
  • Thank you! That worked, kinda? It atleast runs my php script now, but returns me the following error: No screen session found. – Jeremy Sayers Apr 19 '12 at 01:56
  • I want to send a command to the screen when I first create it, something simple like "free -m" to find out if it works first would be great, because it's not working for me – Jeremy Sayers Apr 19 '12 at 01:59
  • That sounds like the ssh connection worked but screen is giving you an error back. Are you sure your arguments are correct. Can you try adding "-p 0 -X" at the end of the string to see if that changes anything? – David Z. Apr 19 '12 at 02:00
  • You can send another command by running ssh2_exec again. So you can try running ssh2_exec($con, "free -m") afterwards, but that sounds like a different issue to the string escaping question. – David Z. Apr 19 '12 at 02:02
  • When I added just "-p 0 -X" to the end it said Please specify a command. I want to be able to create a screen and execute a command to it right away, and then later be able to access the screen and send commands to it also – Jeremy Sayers Apr 19 '12 at 02:06
  • I'm not a screen expert but looking at the usage, isn't the command (i.e. start top) "screen -d -m -S mysession top" and then to resume you can run "screen -R mysession". Where mysession is a friendly name that you give for the session. – David Z. Apr 19 '12 at 02:14
  • So that worked! With just the top but when I tried to do a command with an argument "free -m" it did not create the session – Jeremy Sayers Apr 19 '12 at 02:22
  • Try "screen -d -m -S mysession \"free -m\"" – David Z. Apr 19 '12 at 02:26
  • Hmmm didnt seem to do anything, I did: if (!($stream = ssh2_exec($con, "screen -d -m -S mysession \"free -m\"" ))) { – Jeremy Sayers Apr 19 '12 at 02:33
0

You only need to change "\ to \".

prolink007
  • 33,872
  • 24
  • 117
  • 185
Shaikh Farooque
  • 2,620
  • 1
  • 19
  • 33