Php running linux commands

Executing unix shell commands using PHP

A text box will be used to capture the command. I’ve been told that I have to use the exec() function to execute UNIX shell commands. Something like this, user types ls in the text box. The exec() function will execute the UNIX command and the command will be displayed on the web page. What I want to know how will I get the output of the shell command and display in the web browser using PHP. I don’t know where to start since I’m very new to PHP. I’m using Ubuntu.

You’re probably going to hear this a lot, and it may not even be relevant, but hoo boy does this sound dangerous.

You obviusly want this for a personal project, but as stated you should understand that someone can format your partitions from 1 simple command

Even though this is dangerous, I don’t think this should be downvoted because of that alone. It is better to educate then to scare!

7 Answers 7

Real developers use proc_open ! It has the major and distinct advantage of giving you three PHP streams to feed data into the process, and read both stdout and stderr . This is something that the other process execution functions simply don’t do well.

It comes at the small cost of some boilerplate code, so it’s a bit more verbose. I consider the trade-off to be excellent.

Oh, and running arbitrary commands from your users is perhaps one of the greatest security risks that you could ever conceive of, but I kind of assume you know this by now.

You could start looking at the php manual:

But like sdleihssirhc mentioned, watchout this IS very dangerous and you should NOT allow everything to be executed!
If you still want to do it, to get the output of the shell, just use

exec
The output of the shell will be passed in the second parameter.

exec('ls -la', $outputArray); print_r($outputArray); 

Источник

Execute Shell Command in PHP using exec()

The PHP script is mainly used for developing web applications but it can be used for other purposes also. PHP has some built-in functions to execute system-related commands. exec() is one of them. It is used to execute shell commands or any program from the PHP script. How this function can be used in PHP are shown in this tutorial.

Читайте также:  Mac address on linux ubuntu

Syntax:

This function can take three arguments. The first argument is mandatory that will take the system command. The other two arguments are optional. The second argument is used to store the output of the command in an array. The third argument of this function is used to store the return status of the executed command. This function returns the last line from the executed command output.

Example-1: Use of exec() function without optional arguments

The basic use of the exec() function has shown in this tutorial. Create a PHP file with the following script to know how the exec() function returns the command output. ‘pwd‘ command has used in the first exec()command of the script that returns one line of output. ‘ls -la‘ command has been used in the second exec() command that can return multiple lines of output. If any command returns multiple lines then the output will show the last line as the output.

//Store the output of the executed command

//Store the last line of the executed command

The following output will appear after running the above script from the server. ‘pwd‘ command returns the current working directory as output that is shown in the first output. ‘ls -la‘ command returns the details information of the list of directories and the second output shows the last line from the command output.

Example-2: Print all values of the executed command

In the previous example, no optional argument is used in the exec() function. The following example shows the use of optional arguments of the exec() function. Create a PHP file with the following script. Two optional arguments of exec() are used in this script. ‘ls -l‘ command is used in the first argument that returns the list of directories. $output variable is used here to store the output of the command in an array. $status variable is used to store the return status value of the executed command. The output of the command will be printed as an array and each value of the output array will be printed by using the ‘for’ loop.

//Store the output of the executed command in an array

//Print all return values of the executed command as array

//Print the output of the executed command in each line

//Print the return status of the executed command

The following output will appear after running the above script from the server. The output shows the array that contains the output of the command, ‘ls -l’ and each value of the array in each line.

Example-3: Print all PHP files of the current directory

The following example shows the list of all PHP files of the current directory by using the exec() function. Here, the ‘ls -l *.php‘ command is used here to find out the list of all PHP files of the current directory. tag is used in the script to print the content of the array with the structured format.

Читайте также:  Check permissions linux command

//Store the output of the executed command in an array

//Print the output of the executed command

The following output will appear after running the above script from the server.

Example-4: Run a bash script

How any bash script can be executed by using the exec() function has shown in this example. Create a bash file named loop.sh with the following script that will print all even numbers from 1 to 20.

#!/bin/bash

#Initialize the counter

counter = 1

#Iterate the loop until the $counter value is less than or equal to 20

while [ $counter — le 20 ]

#Print the even numbers

if [ [ $counter % 2 — eq 0 ] ]

#Print $counter without newline

echo » $counter «

#Increment $counter by 1

( ( counter ++ ) )

Create a PHP file with the following script to run the bash script. ‘bash loop.sh‘ is used as the first argument of the exec() function that will execute the loop.sh script file. ‘foreach‘ loop is used to print each value of the $output with space.

echo «All even numbers within 1-20 are:
» ;

//Print the ourput using loop

The following output will appear after running the above script from the server. The output shows all even numbers within 1 to 20.

Example-5 : Run `dir` command using exec() function

‘dir’ command works like the ‘ls’ command. The following example shows how the ‘dir’ command can be executed using a PHP script. Create a PHP file with the following script that stores the output of the ‘dir’ command in the array named $output and the status value in the variable named $return. var_dump() function is used here to print the structure of the $output array with the data type information.

//Print the return status value

echo «The return value of the `dir` command is $return \n » ;

The following output will appear after running the above script from the server.

Conclusion:

Different uses of the exec() function have been explained in this tutorial to help the PHP coders to know the way to execute the shell command by using the PHP script. Some other functions also exist in PHP to do the same type of task.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How to Execute Shell Commands in PHP

There are times when you need to run a shell command from inside a PHP program. This can be for several reasons such as running terminal-based programs or running common terminal commands like ls . PHP can execute any shell command and return its output as a string or array if needed.

Читайте также:  Skype для linux ubuntu one

In this tutorial, we will learn how to execute shell commands in PHP with examples.

PHP exec() Function Syntax

exec(command, output, return_variable) 

Here is what each parameter is used for in exec()

  • command — the only required argument in exec() . It is the terminal command to run
  • output — do something with the output of the command. Can be used to return the output back into the PHP program as a string
  • return_variable — put the output in the variable defined in this parameter

Make a Directory Using exec()

In this example, we will create a directory. We will do this using the Linux mkdir command and passing in the name of the directory we want to create.

The above command will create a directory in the same directory as the PHP file that executed it.

To create a directory in a different location on the system pass in a path before the name of the directory. This can be an absolute path (relative to root) or a relative path to the location of your program.

exec('mkdir /var/www/sites/skillsugar/public/test'); 

Using a Variable inside exec()

It is possible to pass variables into exec() by closing the ‘ (single quote) or » (double quote) and concatenating the variable in using a . (dot).

$file_name = 'new_dir'; exec('mkdir /home/vagrant/sites/skillsugar/public/' . $file_name); 
$dir_name = 'new_dir'; exec('mkdir ' . $dir_name . '/file.txt'); 

Printing the Output from exec()

You can simply echo the output.

Multi-line Output from exec()

You will notice that the above example only prints the last line from the ls command. To get all the lines we can set an output variable in the second parameter of exec() and then print the array containing all the lines.

Array ( [1] => build [2] => cgi-bin [3] => css [4] => error_log [5] => example.txt #. [16] => test [17] => test.php [18] => test2 [20] => translations [21] => web.config ) 

Set a Return Variable

To get what the terminal command returned we can set a variable in the third parameter of exec() .

Return Output as a String with the PHP shell_exec() Function

To return a complete string from a shell execute command in PHP use the shell_exec() function instead. It works in the same way to exec() except it takes no arguments and will return the complete output as a string.

build cgi-bin css error_log example.txt #. test test.php test2 translations web.config 

PHP system() Function

The system() PHP function works just like shell_exec() except you don’t need to print or echo the output string as it does this for you.

build cgi-bin css error_log example.txt #. test test.php test2 translations web.config 

Conclusion

You now know how to execute a shell command in three different ways using PHP. exec() give you the most flexibility, but you will have to pass a second argument to get the complete output as a string. system() is the most basic way to execute a command and get the complete output but has limited functionality.

Источник

Оцените статью
Adblock
detector