#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0

    Proc_open(sc) Windows


    Morning. Im trying to check the state of a service on distant machine, currently im using exec but if machine is unreachable exec sc tries to connect to service forever, without the idea on how to put timeout on exec i found proc_open proc_terminate combination but im having a hard time making it run. I copied the code twiked it a bit but i still have no idea of whats happening with this code. What i want is to run SC and get the result as php variable, in worst case get the output of SC into a file. Currently i have no idea how to make it work the code bellow doesnt write anything to error.txt

    PHP Code:
    $descriptorspec = array(
        
    => array("pipe""r"),
        
    => array("pipe""w"),
        
    => array("file""lerror.txt""a") ); 
    // Create child and start process 
    $child = array("process" => null"pipes" => null); 
    $child["process"] = proc_open("sc \\999.999.999.999 query Spooler"$descriptorspec$child["pipes"]); 
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Make sure you have write permissions to create/edit lerror.txt.

    Is the process being created? Check ps if you're not sure. And is $child[process] a resource (it worked) or false (it didn't)?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by requinix
    Make sure you have write permissions to create/edit lerror.txt.

    Is the process being created? Check ps if you're not sure. And is $child[process] a resource (it worked) or false (it didn't)?
    PS ? the code is running on windows machine. And yes the process is being created .
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Right, Windows. That'd be why I recognized the name "sc" but couldn't find documentation for it. So yeah, substitute Task Manager or tasklist for ps (which, incidentally, could mean PowerShell (which, incidentally, could also tell you the list of processes)).

    So what's the problem? Are you sure something is supposed to be written to stderr at all? Are you reading anything from the #1 pipe?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by requinix
    Right, Windows. That'd be why I recognized the name "sc" but couldn't find documentation for it. So yeah, substitute Task Manager or tasklist for ps (which, incidentally, could mean PowerShell (which, incidentally, could also tell you the list of processes)).

    So what's the problem? Are you sure something is supposed to be written to stderr at all? Are you reading anything from the #1 pipe?
    Based on my lack of knowledge on pipes i should assume that first one is what the sc returns, if it is then yes , well it does if i run it by hand or with exec. Im confused about the fact that there are 3 pipes and not one straight to error.txt
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Two pipes.

    pipes[0] is sc's stdin which you would write to... if you had anything to write. You don't so fclose() it immediately.

    pipes[1] is sc's stdout which you should read from. Presumably sc will output something through stdout so you need to read it. As long as you don't have to worry about writing to sc, stream_get_contents the pipe: sc will write whatever it wants and quit, closing the pipe, and you'll get everything it outputted.

    You set $descriptorspec[2] to be a file and not a pipe, so you don't have to do anything there - it's all managed behind the scenes for you.

    Try
    PHP Code:
    $descriptorspec = array(
        
    => array("pipe""r"),
        
    => array("pipe""w"),
        
    => array("file""lerror.txt""a") );

    // Create child and start process
    $child = array("process" => null"pipes" => null);
    $child["process"] = proc_open("sc \\999.999.999.999 query Spooler"$descriptorspec$child["pipes"]);

    fclose($child["pipes"][0]); // nothing to write
    echo stream_get_contents($child["pipes"][1]); // output sc's output 
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by requinix
    Two pipes.

    pipes[0] is sc's stdin which you would write to... if you had anything to write. You don't so fclose() it immediately.

    pipes[1] is sc's stdout which you should read from. Presumably sc will output something through stdout so you need to read it. As long as you don't have to worry about writing to sc, stream_get_contents the pipe: sc will write whatever it wants and quit, closing the pipe, and you'll get everything it outputted.

    You set $descriptorspec[2] to be a file and not a pipe, so you don't have to do anything there - it's all managed behind the scenes for you.

    Try
    PHP Code:
    $descriptorspec = array(
        
    => array("pipe""r"),
        
    => array("pipe""w"),
        
    => array("file""lerror.txt""a") );

    // Create child and start process
    $child = array("process" => null"pipes" => null);
    $child["process"] = proc_open("sc \\999.999.999.999 query Spooler"$descriptorspec$child["pipes"]);

    fclose($child["pipes"][0]); // nothing to write
    echo stream_get_contents($child["pipes"][1]); // output sc's output 
    Thank you for the explanation , today i got a bit smarter. Will try the code tomorrow.

IMN logo majestic logo threadwatch logo seochat tools logo