Skip to content

Instantly share code, notes, and snippets.

@PhilKershaw
Created November 23, 2011 15:57
Show Gist options
  • Save PhilKershaw/1389041 to your computer and use it in GitHub Desktop.
Save PhilKershaw/1389041 to your computer and use it in GitHub Desktop.
Simple code for executing commands on a remote Linux server via SSH in PHP
<?php
/**
* Simple code for executing commands on a remote Linux server via SSH in PHP
*
* @author Phil Kershaw (In collaboration with Philip Mott)
*
* Note: ssh2_connect requires libssh2-php which is a non-standard PHP lib.
* Debian: apt-get install libssh2-php
* Redhat: yum install libssh2-php ???? I've no idea for redhat, sorry.
*/
$server = "server"; // server IP/hostname of the SSH server
$username = "root"; // username for the user you are connecting as on the SSH server
$password = "root"; // password for the user you are connecting as on the SSH server
$command = "tail /var/log/access.log"; // could be anything available on the server you are SSH'ing to
// Establish a connection to the SSH Server. Port is the second param.
$connection = ssh2_connect($server, 22);
// Authenticate with the SSH server
ssh2_auth_password($connection, $username, $password);
// Execute a command on the connected server and capture the response
$stream = ssh2_exec($connection, $command);
// Sets blocking mode on the stream
stream_set_blocking($stream, true);
// Get the response of the executed command in a human readable form
$output = stream_get_contents($stream);
// echo output
echo "<pre>{$output}</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment