exec() публичный метод

If $callback is set to false then \phpseclib\Net\SSH2::_get_channel_packet(self::CHANNEL_EXEC) will need to be called manually. In all likelihood, this is not a feature you want to be taking advantage of.
public exec ( string $command, Callback $callback = null ) : string
$command string
$callback Callback
Результат string
Пример #1
1
 /**
  * @param string $commandName
  * @param string $target
  * @param array  $targetConfig
  * @param array  $inputCommand
  * @param array  $userHomeDir
  * @return string
  */
 public function executeCommand($commandName, $target, $targetConfig, $inputCommand, $userHomeDir)
 {
     $remoteCommand = str_replace([sprintf('\'%s\'', $commandName), sprintf('target=\'%s\'', $target)], [$commandName, sprintf('root=%s', $targetConfig['root'])], $inputCommand);
     $remoteCommand = sprintf('%s %s', $targetConfig['console'], $remoteCommand);
     $key = null;
     if (array_key_exists('password', $targetConfig)) {
         $key = $targetConfig['password'];
     }
     if (!$key) {
         $key = new RSA();
         if (array_key_exists('passphrase', $targetConfig['keys'])) {
             $passphrase = $targetConfig['keys']['passphrase'];
             $passphrase = realpath(preg_replace('/~/', $userHomeDir, $passphrase, 1));
             $key->setPassword(trim(file_get_contents($passphrase)));
         }
         $private = $targetConfig['keys']['private'];
         $private = realpath(preg_replace('/~/', $userHomeDir, $private, 1));
         if (!$key->loadKey(trim(file_get_contents($private)))) {
             return $this->getTranslator()->trans('commands.site.debug.messages.private-key');
         }
     }
     $ssh = new SSH2($targetConfig['host'], $targetConfig['port']);
     if (!$ssh->login($targetConfig['user'], $key)) {
         return sprintf('%s - %s', $ssh->getExitStatus(), $ssh->getErrors());
     } else {
         return $ssh->exec($remoteCommand);
     }
 }
Пример #2
0
 public function refreshHardware()
 {
     $this->view = false;
     $db = $this->di['db']->sql(DB_DEFAULT);
     $sql = "SELECT * FROM `mysql_server` WHERE `key_public_path` != '' and `key_public_user` != ''";
     $res = $db->sql_query($sql);
     while ($ob = $db->sql_fetch_object($res)) {
         echo $ob->ip . "\n";
         $ssh = new SSH2($ob->ip);
         $key = new RSA();
         $key->loadKey(file_get_contents($ob->key_public_path));
         if (!$ssh->login($ob->key_public_user, $key)) {
             echo "Login Failed";
             continue;
         }
         $memory = $ssh->exec("grep MemTotal /proc/meminfo | awk '{print \$2}'");
         $nb_cpu = $ssh->exec("cat /proc/cpuinfo | grep processor | wc -l");
         $brut_memory = $ssh->exec("cat /proc/meminfo | grep MemTotal");
         preg_match("/[0-9]+/", $brut_memory, $memory);
         $mem = $memory[0];
         $memory = sprintf('%.2f', $memory[0] / 1024 / 1024) . " Go";
         $freq_brut = $ssh->exec("cat /proc/cpuinfo | grep 'cpu MHz'");
         preg_match("/[0-9]+\\.[0-9]+/", $freq_brut, $freq);
         $frequency = sprintf('%.2f', $freq[0] / 1000) . " GHz";
         $os = trim($ssh->exec("lsb_release -ds"));
         $distributor = trim($ssh->exec("lsb_release -si"));
         if (empty($os)) {
             $os = trim($ssh->exec("cat /etc/centos-release"));
             $distributor = trim("Centos");
         }
         $product_name = $ssh->exec("dmidecode -s system-product-name");
         $arch = $ssh->exec("uname -m");
         $kernel = $ssh->exec("uname -r");
         $hostname = $ssh->exec("hostname");
         $swapiness = $ssh->exec("cat /proc/sys/vm/swappiness");
         /*
          $system = $ssh->exec("uptime");// get the uptime stats
         
          $uptime = explode(" ", $system); // break up the stats into an array
         
          $up_days = $uptime[4]; // grab the days from the array
         
          $hours = explode(":", $uptime[7]); // split up the hour:min in the stats
         
          $up_hours = $hours[0]; // grab the hours
          $mins = $hours[1]; // get the mins
          $up_mins = str_replace(",", "", $mins); // strip the comma from the mins
         
          echo "The server has been up for " . $up_days . " days, " . $up_hours . " hours, and " . $up_mins . " minutes.";
         */
         $sql = "UPDATE mysql_server SET operating_system='" . $db->sql_real_escape_string($os) . "',\r\n                   distributor='" . trim($distributor) . "',\r\n                   processor='" . trim($nb_cpu) . "',\r\n                   cpu_mhz='" . trim($freq[0]) . "',\r\n                   product_name='" . trim($product_name) . "',\r\n                   arch='" . trim($arch) . "',\r\n                   kernel='" . trim($kernel) . "',\r\n                   hostname='" . trim($hostname) . "',\r\n                   memory_kb='" . trim($mem) . "', \r\n                   swappiness='" . trim($swapiness) . "' \r\n                   WHERE id='" . $ob->id . "'";
         $db->sql_query($sql);
     }
 }
Пример #3
0
 /**
  * @param string $sourceIdentifier
  * @param string $targetIdentifier
  * @return bool
  */
 public function copy($sourceIdentifier, $targetIdentifier)
 {
     $result = $this->ssh->exec('cp -RPf ' . escapeshellarg($sourceIdentifier) . ' ' . escapeshellarg($targetIdentifier));
     return $result === '';
 }
Пример #4
0
 private function deleteVirtualHost($filehosts)
 {
     $this->info("Deleting virtualhost");
     $this->error('Attenzione il virtual host di ' . $this->workbenchSettings->requested['domain']['valore'] + ' verrà eliminato.');
     $apachedir = "/var/www/html/";
     $ssh = new SSH2($this->workbenchSettings->requested['sshhost']['valore']);
     //ToDo
     if (!$ssh->login($this->workbenchSettings->requested['sshuser']['valore'], $this->workbenchSettings->requested['sshpassword']['valore'])) {
         exit('SSH login failed at ' . $this->workbenchSettings->requested['sshuser']['valore'] . '@' . $this->workbenchSettings->requested['sshuser']['valore']);
     }
     $ssh->exec('a2dissite ' . $this->workbenchSettings->requested['domain']['valore']);
     $ssh->exec('/etc/init.d/apache2 reload');
     $ssh->exec('rm /etc/apache2/sites-available/' . $this->workbenchSettings->requested['domain']['valore'] . '.conf');
     $this->info("Virtualhost deleted");
     if ($filehosts) {
         $this->removeToFileHosts($ssh);
     }
 }
Пример #5
0
 function ssh2()
 {
     $ssh = new SSH2('10.0.51.117');
     if (!$ssh->login('root', 'zeb33tln')) {
         exit('Login Failed');
     }
     echo $ssh->exec('pwd');
     echo $ssh->exec('ls -la');
     echo $ssh->exec('whereis screen');
 }