How do I run a terminal command within PHP? - php

I'm writing an in-house module to generate a small LaTeX PDF. Within the module, how do I use PHP to utilize the command line?

I think what you are looking for is the function: passthru()
if you want to directly send the output of your command.
Or if you run your command that command creates a file in the sever and then you send that file to the user use the command exec()

Related

PHP exec in foreground?

I'm trying to open a Windows program from PHP using exec() on a local machine. Is it possible to start a system program (on Windows 10 if it's relevant) that runs in the foreground using PHPs exec function?
This line:
exec("C:/Windows/notepad.exe 2>&1");
Causes Microsoft's Notepad to open in the background (verified it is actually running using task manager) but I have no access to it, i.e., it doesn't open a window. How do I get it to run in the foreground so I can actually see it and interact with it?
So this seems like an utter ball ache to achieve using exec() for your average coder. There's another way to achieve this result: Have PHP generate .bat files using file_put_contents() with instructions to open a given file path and then auto-delete itself, like so:
#echo off
Start "" "C:\Path\To\File\SomeFile.txt"
del %0
This method requires some kind of task scheduler to monitor a given folder and execute the batch files as they come in. I believe PowerShell can do this, and possibly the Windows Task Scheduler. I think Linux has Cron.

Can PHP command a third party CLI?

I want to extend the automation of the PacketETH program CLI using PHP
It can be done in GUI however this still means a user has to do it
Is it possible to have the packetETH run and a PHP deliver instructions, and then receive results back for manipulation?
In a broader sense, is this type of connection possible at all?
Thankyou
You can access the command line from php by using the Php System Program Execution functions. http://php.net/manual/en/book.exec.php. You can try out the exec function. It lets you execute shell commands.
To run the packeteth program from php you can use a command like the following:
exec("/path/to/packeteth/packETHcli -i lo -m 1 -f packet1.pca");

PHP + SSH: Interactive Shell

I am using phpseclib to run commands via SSH on another server. I am currently using the Interactive Shell example (See Here)
If I want to send a command, I use $ssh->write("ls -la\n"); and then I run $ansi->appendString($ssh->read()); echo $ansi->getScreen(); to see the screen output.
Is there a way for me to run commands from a form where can I use it like a web-based console?
Yes why not! Then you have to implement a form and send the command to your server. But there is a much easier way. Perhaps with ajax and fetch the return from your command line.
http://www.web-console.org/
There are a lot of projects doing exact that. When you build that on your own you have to look at security and much more.

Running shell commands using PHP script

I'm creating an app using CodeIgniter, but I'm unable to run Linux commands using my PHP script.
How do I run terminal commands?
I've tried shell_exec() and exec(), but both don't work while using CI.
That depends on your server configuration if the functions are disabled, you need to enable them in your php.ini. Here are some alternatives:
popen()
or if you need an interactive way try proc_open()
system()

shell_exec question

Say I want to execute an ant command on a linux server using php on a website...
Can I do say, /home/user/apache-ant-1.8.2/bin/ant compile - where compile is the command?
Basically I need to start up a few things with ANT in a few different directories. I would like to do this from a php webpage, can shell_exec perform this?
Yes, shell_exec can run any command (with appropriate permissions).

Categories