3

I have a php from where I have to call a cmd, and from that cmd I have to start an exe using psexec. My php, cmd, psexec.exe and the exe I actually need to run are in the same folder.

php:

shell_exec("runas.cmd");

runas.cmd:

@echo off
psexec -u userName -p password my.exe

If I run the php I can see in the task manager that PSexex.exe is stared but my.exe is not. Also if I run runas.cmd by double-clicking on it, everything is just fine.

fishmong3r
  • 1,414
  • 4
  • 24
  • 51

1 Answers1

2

add 2>&1 after the command and print shell_exec response to see whats actually happen

$a = shell_exec('runas.cmd 2>&1');
var_dump($a);
SkyFox
  • 36
  • 6
  • I opened the php in my browser, but no response whatsoever. It keep loading. – fishmong3r Jun 24 '15 at 09:08
  • keep loading because shell_exec waiting for response which will happen when runas.cmd finish no matter with error or the way its suppose to work. – SkyFox Jun 24 '15 at 09:38
  • It's now running for over 35 minutes. – fishmong3r Jun 24 '15 at 09:44
  • just stop script php execution time not working properly when need to wait something for output as is in your case which mean script will work until you stop it or command which you execute return something i think in you case you need to debug command you execute in console not with double click then you will see what actualy happen when you execute runas.cmd – SkyFox Jun 24 '15 at 09:57
  • I don't get it. If the only difference between the working and the not working scenario is the way I start the runas.cmd what shall I debug? – fishmong3r Jun 24 '15 at 10:03
  • i will try to discribe process as simple as i can php script is executed line by line if you have 5 line and you second line is shell_exec script stoping and waiting shell_exec to return response until response come nothing happen script just waiting and in the end you will get response when command you execute finish what its doin try some other command dir or ls and will see and another problem you can heve when execute command will be if executed command want you to write some information example for this in you case probably will be if you remove password after -p(must ask you to type it) – SkyFox Jun 24 '15 at 11:01
  • No offense but I next time I would appreciate a little higher number of punctuation marks. Though I guess I understand what you are saying. So psexec is waiting for some input? It should be either `runas.cmd` or `psexec` as `my.exe` is not ever running. So how it is possible that any of these scrips are either throwing an error or waiting for input? Like I said if I run them from anwhere else other than php they are just run. No input/output/intervention whatsoever is needed. – fishmong3r Jun 24 '15 at 11:11
  • Sorry about punctuation not enought char length :) from php perspective program/command just still running and this is the reason nothing happen no response or error is returned. You need to see whats happen with this command/program just open cmd/console and execute same command and tell me whats happen – SkyFox Jun 24 '15 at 11:27
  • There you go: `PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich Sysinternals - www.sysinternals.com Couldn't install PSEXESVC service: Access is denied.` – fishmong3r Jun 24 '15 at 11:44
  • finally getting somewhere :) I suppose you will manage to fix the problem now. I have one suggestion to you remove/comment "@echo off" atleast until you are sure all work as except to work – SkyFox Jun 24 '15 at 11:56
  • Don't raise you glass too soon. :) Have to find out which user gets access denied. It should be the one trying to run psexec, which i guess is some php/apache user? – fishmong3r Jun 24 '15 at 12:02
  • :D i dont want to lie you but if i not mistake php use www-data something like probably when you execute command from php uses same user – SkyFox Jun 24 '15 at 12:15
  • Theoretically this code returns the user: `"; phpinfo(); ?>` It's returning SYSTEM. – fishmong3r Jun 24 '15 at 12:17
  • actually both get_current_user and getmyuid give you script owner which is no help to you you have 2 cases here 1 if you execute script with in browser which will use i think was www-data user which is apache or php user and if you run it from console then will use user which you are logged easiest solution add 777 to runas.cmd – SkyFox Jun 24 '15 at 12:26
  • The issue is even if I open up cmd as administrator and copy the content of the runas.cmd and hit enter, I get the same error. – fishmong3r Jun 24 '15 at 12:54