-2

I want to execute a command on my Linux, triggerd by PHP/HTML. Thats my code:

            <td> Status: </td>
            <?php  
                $kommando = "asterisk -rx \'database get SW OFFICE-OPEN\'  \> /etc/asterisk/abstatus.txt";
                shell_exec($kommando);          
            ?>
            <td>

The problem is that the $kommando String is getting stopped at \>, the rest is just drawn on my Site.

First I was using > only, then as \> - nothing worked.

Can you help me please?

  • 6
    you have too many \'s – Funk Forty Niner Apr 20 '16 at 13:52
  • `htmlentities()` ? – mario Apr 20 '16 at 13:52
  • The syntax for the Command is: asterisk -rx 'database get SW OFFICE-OPEN' - so I need to have those right? Otherwise it will cut it? – VitaminAqua Apr 20 '16 at 13:54
  • What a mess! Doing things things without **prior understanding** what it is needed for is pointless. You will make it even worse. Do you know why you need to use `\`? Do you know when? do you know for what characters? If not -> google "escaping" or go to[Wiki](https://en.wikipedia.org/wiki/Escape_character) first – Marcin Orlowski Apr 20 '16 at 13:54
  • if HTML-strings in PHP had to look like this, I would have never touched PHP. – low_rents Apr 20 '16 at 13:55
  • 1
    Addendum to my first comment. For example: `$ergebnis = "\

    Aktiv \

    ";` that should read as `$ergebnis = "

    Aktiv

    ";` or `$ergebnis = '

    Aktiv

    ';`
    – Funk Forty Niner Apr 20 '16 at 13:56
  • Instead of fixing it by yourself, use `escapeshellargs` function – Nadir Apr 20 '16 at 13:57
  • Marcin Orlowski thanks for beeing NOT helpful at all, since I am a beginner how about beeing helpful instead of a bitchy little kid? And it doesnt cut in the HTML strings, it cuts in the first one with the linux code – VitaminAqua Apr 20 '16 at 13:58
  • @VitaminAqua That's how you treat people giving you free help? Feel free to visit http://stackoverflow.com/jobs if you don't like what you get here. – Mike Apr 20 '16 at 14:00
  • If the html strings are not relevant, you should remove them; just put the code that causes the problem and perhaps a bit of context. – jeroen Apr 20 '16 at 14:01
  • He wasnt helping at all. – VitaminAqua Apr 20 '16 at 14:03
  • Don't forget to tag people you want to be notified of your reply by putting an @ symbol before their name (tab auto-completes it). Like this: @MarcinOrlowski. – Mike Apr 20 '16 at 14:04
  • @Mike, thats good to know. Thanks. I edited it, now it should be clear :) – VitaminAqua Apr 20 '16 at 14:07
  • @VitaminAqua Can you explain what you mean by *String is getting stopped at `\>`*? How do you determine this? What would be the expected behavior? – Mike Apr 20 '16 at 14:11
  • @Mike after the \> the PHP script is printed to the screen – VitaminAqua Apr 20 '16 at 14:13
  • Get rid of those backslashes. There is nothing special about `<`, `>` or `'` in a double-quoted PHP string. Those need not to be escaped. – ROAL Apr 20 '16 at 14:14
  • @ROAL I need them in order to work correctly. It is a Linux command, it needs those ' – VitaminAqua Apr 20 '16 at 14:15
  • `after the \> the PHP script is printed to the screen` OK, so I'm flagging as duplicate of http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page **edit** Already voted to close for another reason in case someone else wants to flag it as dupe. – Mike Apr 20 '16 at 14:20
  • @VitaminAqua Yes, those remain there, but the slashes go away, like so: `$kommando = "asterisk -rx 'database get SW OFFICE-OPEN' > /etc/asterisk/abstatus.txt";` – ROAL Apr 20 '16 at 14:24
  • @ROAL okay, thanks. But that, sadly, didnt fix the issue. :( – VitaminAqua Apr 20 '16 at 14:28
  • @Mike I know, but I got a feeling (still, feel free to correct me with this) that all who posted a comment to this question will be notified anyway. Direct notification with `@` will be needed if someone is not there yet only. But I need to check that - maybe I imagined this :) – Marcin Orlowski Apr 20 '16 at 14:34
  • @MarcinOrlowski Pretty sure you're imagining things hehe. I even posted a [question on Meta.SE](http://meta.stackexchange.com/questions/261541/notify-new-users-when-their-comment-will-not-ping-anyone) about this. – Mike Apr 20 '16 at 14:41
  • @Mike Ok, thanks. I updated my internal neural net :) – Marcin Orlowski Apr 20 '16 at 15:36
  • @VitaminAqua Try to put the relevant part into a separate script and run it. Only put there the variable and `shell_exec` function, nothing else, no HTML, no other PHP. Also enable errors with `error_reporting(E_ALL)` at the beginning of your script, in case they're disabled. Just to see if the problem really is somewhere there. – ROAL Apr 21 '16 at 06:27

1 Answers1

0

I think your forgetting to escape the closing 'P', so use:

 \<\/P\>

instead of

 \</P\>
Doug
  • 1,850
  • 23
  • 50
  • 1
    Note: The question has been edited and it appears this answer is no longer relevant. Also a closing paragraph tag should be ``, not `<\P>` – Mike Apr 20 '16 at 14:09
  • Good spot, it's easy to get the wrong way while typing on mobile, where on keyboard you wouldn't even think about it. – Doug Apr 20 '16 at 14:10