PHP/Development/System Command

Материал из Web эксперт
Перейти к: навигация, поиск

Calling the man Command

<html>
<head>
<title>Calling the man command.</title>
</head>
<body>
<form>
   <input type="text" value="<?php print  $manpage; ?>" name="manpage">
</form>
<pre>
<?php
if ( isset($manpage) )
     system(  "man $manpage | col -b" );
?>
</pre>
</table>
</body>
</html>



Execute help command and check its output

<?php
$command = "help";
$logged_in = explode(" ", shell_exec($command));
for ($i = 0; $i < count($logged_in); $i++) {
   if ($argv[1] == trim($logged_in[$i])) {
      $found_user = 1;
   }
}
if($found_user) {
   print "User $argv[1] is currently logged in\n";
} else {
   print "User $argv[1] is NOT currently logged in\n";
}
?>



Execute shell command "dir"

<?php
$dir = shell_exec("dir");
$subject = "Half-hourly file report.";
$body = "The following files are in the current directory : \n\n";
$body .= $dir;
echo $body;
?>



Ping command

<?
$command = "ping -c5 www.thickbook.ru";
exec($command, $result, $rval);
for ($i = 0; $i < sizeof($result); $i++) {
echo "$result[$i]<br>";
}
?>



Ping server

<?php
   $server = "www.wbex.ru";
   $count = 3;
   echo "<pre>";
      system("/bin/ping -c $count $server");
   echo "</pre>";
   system("killall -q ping");
?>



Porter scanner

<?php
   $target = "www.wbex.ru";
   echo "<pre>";
      system("/usr/bin/nmap $target");
   echo "</pre>";
   // Kill the task
   system("killall -q nmap");
?>



Using exec() to Produce a Directory Listing

<html>
<head>
<title>Using exec() to produce a directory listing</title>
</head>
<body>
<?php
exec( "ls -al .", $output, $return );
print "<p>Returned: $return</p>";
foreach ( $output as $file )
   print "$file<br>";
?>
</table>
</body>
</html>



Using popen() to Pass Data to the column Application

<html>
<head>
<title>Using popen() to pass data to the column command</title>
</head>
<body>
<?php
$products = array(array( "A", 2, "red" ),
                  array( "B", 3, "blue" ),
                  array( "C", 1, "pink"  ),
                  array( "D", 1, "orange"   )
);
$ph = popen( "column -tc 3 -s / > data.txt", "w" )  or die( "Couldn"t open connection to "column" command" );
foreach ( $products as $prod )
    fputs( $ph, join("/", $prod)."\n");
pclose( $ph );
?>
</table>
</body>
</html>



Using popen() to Read the Output of the UNIX who Command

<html>
<head>
<title>Using popen() to read the output of the UNIX who command</title>
</head>
<body>
<h2>Administrators currently logged on to the server</h1>
<?php
$ph = popen( "who", "r" ) or die( "Couldn"t open connection to "who" command" );
$host="www.wbex.ru";
while ( ! feof( $ph )  ){
    $line = fgets( $ph, 1024 );
    if ( strlen( $line ) <= 1 )
        continue;
    $line = ereg_replace("^([a-zA-Z0-9_\-]+).*", "<a href=\"mailto:\\1@$host\">\\1</a><BR>\n",$line );
    print "$line";
}
pclose( $ph );
?>
</table>
</body>
</html>