There is a PHP library called phpseclib that helps to SSH to a remote server and execute command. We can also get the output and display on the screen.
First, we need to download the phpseclib, it’s available to download via: http://phpseclib.sourceforge.net/
Examples SSH To Remote Server And Execute Command With PHP
<?php include('Net/SSH2.php'); $ssh = new Net_SSH2('domain.com'); if (!$ssh->login('username', 'password')) { exit('Login Failed'); } echo $ssh->exec('ls'); ?> |
<?php include('Net/SSH2.php'); $key = new Crypt_RSA(); //$key->setPassword('whatever'); $key->loadKey(file_get_contents('privatekey')); $ssh = new Net_SSH2('www.domain.tld'); if (!$ssh->login('username', $key)) { exit('Login Failed'); } echo $ssh->read('[email protected]:~$'); $ssh->write("ls -la\n"); echo $ssh->read('[email protected]:~$'); ?> |
+ [download id=”20″ format=”1″] + View our demonstration page to see how it works.