All Linux HowTo's Scripting HowTo's

Send SSH commands from PHP 5.6 (php56w)

Share This:

This article explains how to send SSH commands (SSH, SCP, SFTP) to a remote server using php. In this example we have a working php56w installation with Apache. We’re using CentOS 7. SELinux is enabled.

Download the libraries:

Unzip the files into a new library directory:

# Go to the document root and create the library directory
cd /var/www/html/
mkdir libraries_ssh

# Put the Zip file in the right place and unzip it
cd libraries_ssh
# put the Zipped file here

# Return to the document root and create a test php script
cd ..
touch example.php

# Make sure Apache owns the files and SELinux is happy
chown apache.apache -R ./
restorecon -rv ./
setsebool -P httpd_can_network_connect on

Now we need to populate “example.php” with some sample code to test our progress.

// test SSH commands

// include the library path
set_include_path(get_include_path() . PATH_SEPARATOR . 'libraries_agix/');

// and the libraries themselves


  other options for SSH related commands:


// connect to the server/pi
$ssh = new Net_SSH2('');

if (!$ssh->login('root', 'MysecretPassword')) {
    exit('Login Failed');

// the actual command to run
echo $ssh->exec('ls -la');


You should be able to test it now.

Interesting links:

Similar Posts:

Leave a Reply

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