Question Locked

PHP System commands

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm trying to execute the following system commands throught php:

system("tracert -d 4.2.2.2 | grep "ms" | gawk "{print ($2+$4+$6)/3 \",\" \"http://who-is.net/php-nslookup.php?host=\"$8\"^&submit=ReverseIP\" \",\" \"Window2\"}" >> trace.txt");

i'm getting an 'invalid syntax' error. It seems to have to do with the quotes, but they're necessary for the command. Any help is appreciated.

thanks.

11 replies

by dr.engnr

this is a windows machine with unxutils installed. most unix commands are available.

by netman1958

I didn't know you could post to a web url from within gawk. Are you sure that is possible? I thought you would have to use curl for that.

by dr.engnr

it's actually just pulling the individual data i need from the tracert and surrounding the $8 column with url text.

by netman1958

Try replacing the double-quotes before tracert and after trace.txt with single quotes. For instance:

system('tracert -d 4.2.2.2 | grep "ms" | gawk "{print ($2+$4+$6)/3 \",\" \"http://who-is.net/php-nslookup.php?host=\"$8\"^&submit=ReverseIP\" \",\" \"Window2\"}" >> trace.txt');

I can't test that right now so let me know the results.

edited to add:
see this article
http://me.abelcheung.org/2009/01/19/windows-process-execution-on-php-with-double-quotes/

by dr.engnr

This seems to be "better". instead of immediately failing it is processing but no file is exported. I have tried assigning the command to a variable $command and printing it. It can be pasted directly into a prompt and runs fine.

is there a reason anyone can see that >> wouldn't work as written?

by dr.engnr

damn. i've tried about every combination of ' and " i can conceive.

by dr.engnr

the text file actually gets created using the following code, but it stays empty even though the script appears to be running.
system('tracert -d who-is.net | grep "ms" | gawk "{print ($2+$4+$6)/3 \",\" \"http://who-is.net/php-nslookup.php?host=\"$8\"^&submit=ReverseIP\" \",\" \"Window2\"}" >> H:\WHO-IS.NET\trace.txt ');

any idea what i have to do?

by netman1958

Since gawk is what is actually creating the output that you want in the file, you need to move your output re-direction(>> H:\WHO-IS.NET\trace.txt) inside the curly braces.

I don't have PHP running on a windows machine so I can't experiment with it here.

by dr.engnr

what a p.i.t.a. just figured out what it was. because i am using unxutils i had to copy the grep.exe and gawk.exe into the c:\windows directory as well as system32. even though the commands function correctly when pasted to a prompt i guess the system() command needs all files in both locations...

fyi

This discussion is closed to new replies.