Last active
July 2, 2021 17:15
-
-
Save sarfraznawaz2005/d9f87556d57b226040152c1c141d7492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Class to connect to remote server via SSH. | |
* | |
* Requires phpseclib/phpseclib package which can be imported via composer. | |
*/ | |
namespace AgentsApp\Libs; | |
use phpseclib\Crypt\RSA; | |
use phpseclib\Net\SSH2; | |
class Ssh | |
{ | |
private $ssh; | |
public function __construct(string $host, int $port) | |
{ | |
$this->ssh = new SSH2($host, $port); | |
$this->ssh->setTimeout(0); | |
} | |
/** | |
* Login via username and password. | |
* | |
* @param string $username | |
* @param string $password | |
* @throws SshException | |
*/ | |
public function login(string $username, string $password) | |
{ | |
if (!$this->ssh->login($username, $password)) { | |
throw new SshException(__CLASS__ . ' : ' . 'Could not login!'); | |
} | |
} | |
/** | |
* Login via username and SSH key. | |
* | |
* @param string $username | |
* @param string $keyPath | |
* @throws SshException | |
*/ | |
public function loginUsingKey(string $username, string $keyPath) | |
{ | |
$key = new RSA(); | |
$key->loadKey(file_get_contents($keyPath)); | |
if (!$this->ssh->login($username, $key)) { | |
throw new SshException(__CLASS__ . ' : ' . 'Could not login!'); | |
} | |
} | |
/** | |
* Login via username and password-protected SSH key. | |
* | |
* @param string $username | |
* @param string $password | |
* @param string $keyPath | |
* @throws SshException | |
*/ | |
public function loginUsingKeyPassword(string $username, string $password, string $keyPath) | |
{ | |
$key = new RSA(); | |
$key->setPassword($password); | |
$key->loadKey(file_get_contents($keyPath)); | |
if (!$this->ssh->login($username, $key)) { | |
throw new SshException(__CLASS__ . ' : ' . 'Could not login!'); | |
} | |
} | |
/** | |
* Runs a command | |
* | |
* @param string $command | |
* @return mixed | |
*/ | |
public function run(string $command) | |
{ | |
return $this->ssh->exec($command); | |
} | |
/** | |
* Runs a command live and outputs results. | |
* | |
* @param string $command | |
* | |
* @param $isBrowser | |
* @return mixed | |
*/ | |
public function runLive(string $command, $isBrowser = false) | |
{ | |
$this->ssh->exec($command, function ($output) use ($isBrowser) { | |
if ($isBrowser) { | |
echo "<pre>$output</pre>"; | |
flush(); | |
ob_flush(); | |
sleep(1); | |
} else { | |
echo $output; | |
} | |
}); | |
} | |
/** | |
* Runs multiple commands at once | |
* | |
* @param array $commands | |
* @return mixed | |
*/ | |
public function runMany(array $commands) | |
{ | |
// 'set -e' to stop on first error | |
$commands = 'set -e;' . implode(';', $commands); | |
return $this->run($commands); | |
} | |
public function isConnected(): bool | |
{ | |
return $this->ssh->isConnected(); | |
} | |
public function isTimeout(): bool | |
{ | |
return $this->ssh->isTimeout(); | |
} | |
public function getLog() | |
{ | |
return $this->ssh->getLog(); | |
} | |
/** | |
* Dynamically call the actual SSH2 client. | |
* | |
* @param string $method | |
* @param array $parameters | |
* @return mixed | |
*/ | |
public function __call($method, $parameters) | |
{ | |
return $this->ssh->{$method}(...$parameters); | |
} | |
} | |
class SshException extends \Exception | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment