Question

I have a linux command line that I need to execute witin either PHP or Javascript (PHP preferred.)

The command is

keygen AB3554C1D1971DB7 \pc_code 365

However, I would like to substitute the \pc_code with a string like $pccode where user enters the generated PC Code. This is for a legit project, but having a problem with creators of the program giving me assitance.

Please help!

Was it helpful?

Solution

It could be that this program is poorly written and outputs its information to stderr instead of stdout. Or it could be that it is failing and (properly) printing the error message to stderr. In either case, shell_exec wouldn't capture stderr. However, you should be able to capture stderr by adding "2>&1" to the end of your command, i.e.:

$result = shell_exec('keygen AB3554C1D1971DB7 \pc_code 365 2>&1');
echo '<pre>'.htmlspecialchars($result).'</pre>';

Edit: For continuity's sake, what fixed the problem was something mentioned in a comment to another answer below:

Maybe it doesn't like the bare backslash? you could try "\\pc_code"

OTHER TIPS

You could use shell_exec():

$cmd = sprintf("keygen AB3554C1D1971DB7 %s 365", 
               escapeshellarg($pccode));
$result = shell_exec($cmd);

Whenever you execute external commands you have to be extremely careful to avoid command injection. You can prevent this using escapeshellarg().

Have a look at exec

Executing a Linux command isn't really possible from JavaScript (nor should it be). Assuming you're referring to JavaScript that is run on a client's web browser, there would be no security model that would keep people safe from malicious command line access. PHP is a different story.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top