PHP Accessing SSH functions

If you don’t have SSH on your hosting account, here’s how you can do many of the Bash script functions, from within PHP.

Alternatively, all the good Bash commands you do repeatedly, could be translated into a PHP program to do the entire sequence of steps automatically.

I’m only showing ls and mysql --help but you could use any Bash script command that is supported in your hosting account.

Go to my PHP site to see PHP Accessing SSH in action.

Here’s how to have a Bash shell script run directly via a URL, see glerner.com/date.sh:

Here’s the Bash shell script referenced:

#!/bin/bash
DATE="$(date)"
echo "Content-type: text/html"
echo ""
echo "<!doctype html>"
echo "<html><head><title>Test</title></head><body>"
echo "<p>Today is $DATE </p></body></html>"

Note: The line ending character must be correct on (at least some) systems. Windows uses CRLF, Linux uses LF; having CRLF on Linux in a shell script gives “/bin/bash^M: bad interpreter: No such file or directory”. See that ^M, that’s the code for the Carriage Return character, entered in ancient times as Ctrl-M; LF, the Line Feed character was Ctrl-J — about the only computer codes still used from the early mainframe printers.

Filezilla connecting in SSL mode converts the line ending characters properly. Filezilla (version 3.6.0.1) connecting in SSH mode does not convert .sh line ending characters properly.

<?php 
$thisDirectory = dirname(__FILE__); include "$thisDirectory/../shared/doctype.php"; ?>
<HTML lang="en">
<HEAD>
<?php $layout="verylong.css"; 
	$docroot = getenv("DOCUMENT_ROOT"); 
	include "$docroot/shared/html5header.php";
?>

<TITLE>Run System Commands in PHP</TITLE>

<meta name="description" content="Run System Commands in PHP">
<meta name="keywords" content="SSH commands in PHP, PHP system commands, php exec commands">

<?php $servnodots = str_replace('.','-',getenv("SERVER_NAME")); ?>

</HEAD>

<BODY id="<?php echo $servnodots;?>">
<DIV id="wrapper_extra">
	<DIV id="wrapper" class="clearfix">
		<DIV id="block_1"> 
			<h1>Run System Commands in PHP</h1>

<p>System() displays results, exec() returns the results to PHP</p>
<?php 
$docroot = getenv("DOCUMENT_ROOT"); 

echo "Try 1: ".exec("$docroot/date.sh");
echo "Try 2: ".exec("sh $docroot/date.sh");
echo "<hr><p>Notice how date is displayed twice? Once from the shell, once from the last line of the result being echo'd</p>";
echo "<p>Date is: ".system("date")."</p>";

echo "<hr><p>Separate Multiple commands with semicolons (;). If specify a 2nd parameter, result lines get appended to that variable.</p><pre>";
$retval = array();
exec("cd $docroot; cd styles; ls -d t*", $retval); /* ampersands between commands doesn't keep the current directory, always runs ls on docroot. Semicolon between commands works */
$retval[]="\n</pre><p>Now Fonts and MySQL Help</p><pre>\n";
/* also works, but displays full path:
	exec("ls -d $docroot/styles/t*", $retval);	
*/
exec("cd $docroot/fonts; ls -d t*; echo \"\n\" ; mysql --help", $retval);	/* works */

foreach ( $retval as $v )
{
echo "$v\n";
}
echo "</pre>";

$send_buffer_size = 4096 ;
$retval = '100';

echo "<hr><p>System displays everything, then returns the last line of the result</p>";
$cmd = "cd $docroot; ls -d w*";
$lastline = system("$cmd", $retval);
echo "<p>Lastline: </p><pre>$lastline</pre>";
echo "<p>Retval: </p><pre>$retval</pre>";

echo "<hr><p>exec displays nothing, returns the entire result, as array of lines of text </p>";
$retval = array();
echo "<pre>";
exec( $cmd, $retval);
foreach ( $retval as $v )
{
echo "$v\n";
}
echo "</pre>";

/* display this file as html */
$filename = $docroot.$_SERVER["PHP_SELF"];
$handle = fopen($filename, "r");
$buffer = fread($handle,$send_buffer_size); 

$encoding='UTF-8';
$html = htmlspecialchars($buffer,ENT_COMPAT,$encoding);
echo '<h2>The exact code to do this:</h2><pre>'.$html.'</pre>';
?>
		</DIV><!-- block-one content -->

	</DIV>
</DIV>

</BODY>
</HTML>

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.