Ssh2Factory.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Factory;
  3. use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;
  4. use phpseclib3\Crypt\PublicKeyLoader;
  5. use phpseclib3\Crypt\RSA;
  6. use phpseclib3\Net\SSH2;
  7. class Ssh2Factory
  8. {
  9. /**
  10. * @param ServerConfigurationRepository $serConf
  11. * @return SSH2
  12. * @throws \Exception
  13. */
  14. public function fromServerConfiguration(ServerConfigurationRepository $serConf){
  15. if(!$serConf->sshHost){
  16. throw new \InvalidArgumentException("SSH Host is empty");
  17. }
  18. $ssh2 = new SSH2($serConf->sshHost, $serConf->sshPort );
  19. $keyOrPassword = $serConf->getSshPassword();
  20. //private key
  21. if($serConf->hasSshKey()){
  22. $keyOrPassword = PublicKeyLoader::load($serConf->getSshKey());
  23. }
  24. //login
  25. if(!$ssh2->login($serConf->sshUser, $keyOrPassword)){
  26. throw new \Exception(sprintf("Login to SSH Host: %s failed", $serConf->sshHost));
  27. }
  28. return $ssh2;
  29. }
  30. }