How to run CLI commands in PHP on another server using SSH?

php ssh github
ssh commands from php
ssh2_exec multiple commands
run php file in ssh
ssh access in php
pure php ssh
check ssh connection php
php connexion ssh

I am trying to run CLI commands in PHP but on a different server. In order to run the command on the other server I am using the linux ssh command. In order to run the CLI command in PHP I am using exec().

This works...

$output = exec('cut -d: -f1 /etc/passwd'); //works!

This works from the command line of one server to another...

ssh "date"

But, when I try to do this in PHP it does not work...

$output = exec('ssh "date"'); //does not work!

Is what I am trying to do possible? How do I do this? Thanks in advance.

ssh2_connect or phpseclib

You can use ssh2_connect or phpseclib. On my EC2 instance I had to install the package for ssh2_connect (e.g. yum install php56-pecl-ssh2-0.12-5.15.amzn1.x86_64) as it wasn't there by default. Note: this could be done with a bash script as well.


$connection = ssh2_connect('localhost', 22);
ssh2_auth_password($connection, 'user', 'password');
$stream = ssh2_exec($connection, 'ls -l > /tmp/worked.txt');

$ssh = new Net_SSH2('');
$ssh->login('username', 'password') or die("Login failed");
echo $ssh->exec('command');

Further reading:

ssh2_exec - Manual, Manipulation · Authentication Services · Command Line Specific Extensions · Compression ssh2_exec — Execute a command on a remote server An SSH connection link identifier, obtained from a call to ssh2_connect(). Here's what I use for doing ssh2_exec for long running scripts, expanding on other examples. Log in to Plesk GUI  as Plesk Administrator Note: It is required to allow logging in on the server under this account and to run PHP over command line. Open the tab  Domains > > Web Hosting Access  and set option Access to the server over SSH to /bin/bash. Connect to the server via SSH as root user.

to connet to your server with ssh you need to use ssh2_exec php function.

I recommed you to use phpseclib, it's a wrapper for PHP SSH implementation

Execute Shell commands via PHP, PHP's capabilities of making SSH connections and executing remote commands I've been using it a lot in PHP CLI applications that I run from cronjobs, but  The SSH client program can be used for logging into a remote machine or server and for executing commands on a remote machine. When command is specified, it is executed on the remote host/server instead of a login shell.


sudo apt install php-ssh2

This example will receive the output of ls -ltrapR from the remote php server.

$con=ssh2_connect('223.245.xx.xx', 22);
ssh2_auth_password($con, 'root', 'bybyDRRxxxx');
$shell=ssh2_shell($con, 'xterm');
$stream = ssh2_exec($con, 'ls -ltrapR');
stream_set_blocking($stream, true);
$out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($out);

Pay attention as it is just using basic auth (Fine trought ssl, better use a vpn). It can be securised furthermore with cryptographic keys and else.

Maintaining your Server with Command Line PHP, In this article we'll look at the advantages of using PHP CLI. up a new terminal window or SSH session to your server or computer and from the To execute the script, all you have to do is type in php test.php and you'll see  If you observe above command, it is similar to regular SSH command with minor difference. We have appended command to be executed (highlighted in red color). When we execute this command. It’ll generate below output: Linux Execute multiple commands. Using this technique, we can execute multiple commands using single SSH session.

How to Execute Linux Commands on Remote System over SSH, commands “hostname” and “date” excute locally and not thru ssh tunnel !!! (local sh split command line on “;” and execute each of them. you can  I have a backup sync program on local server. I have an ssh password less login set up, and I can run commands on an external server in bash script doing: How do I run multiple commands in bash on a remote Unix or Linux server? What is the best way to SSH in and run various unix commands in a bash shell? [donotprint] [/donotprint]There are

Using remote SSH commands, Remote SSH execution: run single commands remotely using the Use CLI tools for tasks like database migration (see below); Use remote task will work php artisan some:command php some-script.php # will _not_ work: . Basic SSH Commands That You Should Know About. In this tutorial, we are going to cover 17 basic SSH commands that you should know about. By learning them, you will understand how to navigate and manage your VPS or server using the command line.

How To Run Multiple SSH Command On Remote Machine And Exit , Explains how to run multiple SSH command on a unix, macOS, FreeBSD, OpenBSD, Linux server using the ssh command line without  If the SSH server is not running, type and execute the sudo systemctl start ssh command. Installing SSH client on Windows OS. There are many SSH clients for Windows OS. For the purpose of this article, we’ll use the 64-bit PuTTY client to connect to the remote MySQL server. Double click on the putty-64bit-<version>-installer.msi installer.

  • Try to add the user name before the remote server ,