Can PHP command a third party CLI? - php

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");

Related

Executing a c program in php on windows

Can any one please tell me how to run(compile and execute) a c file in php on windows(windows 8)?
I know that using exec() i can execute a compiled file but how to compile the c file in php?
I am using xampp.
I have tried with exec() but its not working.
You don't need to compile every time you want to run it. So you would typically compile it once on its own, no need for php, and then use it as you want it.
(You could certainly run the compilation command using exec, but I don't see the point, and I believe you are confused as well.)
If you want to run a source code given from web site, you must compile it, but it can't be done in php. You need a C compiler, for example gcc. Install gcc and from php you call
exec( "/path/to/gcc source_code.c" );

Run PHP script from shell or apache server

I am wondering if a PHP script can be executed from a shell command line.
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux
I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows? all I have at the moment installed is WAMP, which has mysql, php and apache server.
I am sorry for being a novice.
thanks for your kind help
I am wondering if a PHP script can be executed from a shell command line.
Yes
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
It won't have $_REQUEST and friends populated, and $_SERVER won't have server information in it.
Is it better to run a script from shell for performance
Maybe. It avoids the overhead of runnning a webserver. It stops you having cached versions in memory for faster re-execution.
and also is it better to run it from windows or unix/linux
Benchmark it.
I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.
There doesn't seem to be any need to involve a web server for that.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?
The standard Windows shell can.
all I have at the moment installed is WAMP, which has mysql, php and apache server.
You'll need the command line version of PHP. I've no idea if WAMP includes it or not.
I am wondering if a PHP script can be executed from a shell command line.
It's possible either by executing:
$ php -f your_script.php
Or by inserting #/usr/bin/env php into the first line of the script and by making it executable.
$ head -n 1 your_sript.php
#/usr/bin/env php
$ chmod +x your_script.php
$ ./your_script.php
Note: this example only works on UNIX systems.
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
You can use the same Syntax/Functions etc. The only difference is that there are command line arguments in $argv and some other values in the $_SERVER variable.
Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux.
That doesn't really matter. For your usecase you don't really need a webserver, and a full featured GUI. The advantage of having a command line tool is, you can combine your program with other program available like grep etc.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?
You don't need ubuntu, you can also execute a shell script from windows. The PHP executable is located in the %PATH%. This question will help you in order to do this: https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them
Then simply open cmd.exe and execute a script using php -f your_script.php
Yes, PHP can be run from command line.
No, there aren't any differences in coding.
The only difference is that it's not Apache running the script, but the user you are currently logged in as. That could mean different privileges on certain maps and folders.
Yes you can execute PHP from the command line using:
/path/to/php.exe /path/to/script.php
The main difference is that it doesn't run through Apache, so you won't have things that rely on it (like some $_SERVER data).
Also it won't be subject to timeouts on the command line, unless you have a PHP limit set.
Take a look at http://php.net/manual/en/features.commandline.php for more info.

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).

Executing multiple simultaneous php scripts from CLI

I have 55 php files that I would like to run simultaneously from the command line. Right now, I am running them in multiple CLI windows using the code:
php Script1.php
I would like to be able to call one single php file that would execute all 55 php files simultaneously. I have been reading about how to make the command line not wait for the output, but I can't seem to make it work.
This thread:
How to run multiple PHP scripts from CLI
suggests putting an & at the end of the command to run the command in the background, but using the xampp CLI this doesn't seem to do anything.
Any ideas greatly appreciated.
Brian
By mentioning XAMPP, I assume you are on windows. I think what you need is the start command. You probably need start php Script1.php. For more info, do a
start /?|more
Linux
Apart from adding a &, you also need to redirect output to somewhere - otherwise your php process waits until the other process finished, because there could be more output:
exec('/path/to/program & > /dev/null 2>&1')
You could use the php forking mechanism. Read about it here: http://php.net/manual/en/function.pcntl-fork.php

How do I run a terminal command within 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()

Categories