php emulates the ping command (how to use the php exec function)

  • 2020-10-31 21:40:47
  • OfStack

php is used to simulate the methods of DOS command ping. Here, exec, the built-in function of php, is mainly used to call the ping command of the system, so as to realize the function of ping command.

<?php
$to_ping='www.phpernote.com';
$count=2;
$psize=66;
echo " Being performed php ping Command, please wait ...\n<br><br>";
flush();
while(1){
 echo "<pre>";
 exec("ping -c $count -s $psize $to_ping", $list);
 for($i=0;$i<count($list);$i++){
  print $list[$i]."\n";
 }
 echo "</pre>";
 flush();
 sleep(3);
}
?>

Note that using the exec function requires the server to support calling the system's built-in functions. You can also use php built-in functions such as system to do this. The php manual explains these two functions:
exec -- Executes external programs
Grammar: string exec (string command [, array & output [, int & return_var]] )
Description:
exec() executes the given command command, but it doesn't output anything. It simply returns the last line from the result of the command. If you need to execute a command and get all the data from the command, use the passthru() function.
If array have given parameters, the specified array will be command output fill every line 1, note: if the array had already contains some elements of words, 1 exec () will attach it in the back of the array, if you don't want this function, additional elements, you can pass this array to exec () before calling unset ().
If the parameters array and return_var are given, the status command returned to execute will be written to this variable.
If you allow input from the user that can be passed to this function, then you should use escapeshellcmd() to make sure that the user cannot trick (trick) to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it in the background (background), you must make sure that the output of the program goes to (redirected) to a file or some output stream, otherwise PHP will hang (hang) until the end of the program.

system -- Executes external programs and displays output
Grammar: string system (string command [, int & return_var] )
Description:
system() executes the given command command and prints the result. If the parameter return_var is given, the status code of the executing command will be written to this variable.
Note: If you allow input from the user to be passed to this function, then you should use escapeshellcmd() to make sure that the user cannot trick the (trick) system into executing arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it in the background (background), you must make sure that the output of the program goes to (redirected) to a file or some output stream, otherwise PHP will hang (hang) until the end of the program.
Both of these are used to call the system shell command, different:
exec can return all the results of the execution to the $output function (array),$status is the state of the execution 0 for success 1 for failure
systerm does not need to provide the $output function, it simply returns the result, similarly $return_var is executed status code 0 for success 1 for failure
exec example:


<?php  
$a = exec("dir",$out,$status);  
print_r($a);  
print_r($out);  
print_r($status);  
?> 

system example:

<?php  
$a = system("dir",$out);  
print_r($a);  
print_r($out);  
?> 


Related articles: