Search code examples
phpwmic

can't add double forward slashes on exec function php


Currently I have this problem in PHP, I am trying to make a simple webpage that executes some code in the shell and shows the proccess running the code I am trying to execute is this

wmic -U User%Passwd //192.168.1.3 "select Caption from Win32_Process"

In the shell it executes correctly (Ubuntu 14.04 LTS) but in PHP those double slashes are taken as a comment, like in this code

exec('wmic -U User%Passwd //'.$IP.' "select Caption from Win32_Process"',$exit);

However, the double forward slashes (//) are commenting the rest of the code!

How can I rewrite that code in order for PHP to interpret those double slashes not as a comment but as part of the code?

Thank you in advance


Solution

  • You need to escape the slash / char with backslash char \, so your new code will be:

    exec('wmic -U User%Passwd \/\/'.$IP.' "select Caption from Win32_Process"',$exit);
    

    From PHP manual:

    The backslash character has several uses. Firstly, if it is followed by a non-alphanumeric character, it takes away any special meaning that character may have. This use of backslash as an escape character applies both inside and outside character classes.

    For example, if you want to match a "*" character, you write "*" in the pattern. This applies whether or not the following character would otherwise be interpreted as a meta-character, so it is always safe to precede a non-alphanumeric with "\" to specify that it stands for itself. In particular, if you want to match a backslash, you write "\".