Beispiel #1
0
 public function addUserPmaControl()
 {
     $this->layout_name = false;
     $this->view = false;
     fwrite(STDOUT, str_repeat("#", 80) . "\n");
     $text = "Create user pmacontrol, Generating SSH keys and install it !";
     fwrite(STDOUT, "#" . str_repeat(" ", (80 - strlen($text) - 2) / 2) . $text . str_repeat(" ", (80 - strlen($text) - 2) / 2) . "#\n");
     fwrite(STDOUT, str_repeat("#", 80) . "\n");
     $passwdpma = $this->setPasswdPmaControl();
     fwrite(STDOUT, str_repeat("-", 80) . "\n");
     $db = $this->di['db']->sql(DB_DEFAULT);
     $sql = "SELECT * FROM mysql_server";
     $servers = $db->sql_fetch_yield($sql);
     Crypt::$key = CRYPT_KEY;
     $account_valided = [];
     foreach ($servers as $server) {
         fwrite(STDOUT, "try to connect in ssh to " . $server['ip'] . " \n");
         $login = Crypt::decrypt($server['ssh_login']);
         $password = Crypt::decrypt($server['ssh_password']);
         $failed = true;
         $run = 1;
         reset($account_valided);
         /*
          * Try credntial in mysql_server (from db.config.ini)
          * else try last credentials successfully worked
          * to finish set your login // password
          */
         do {
             echo "Trying credential (" . $server['ip'] . " login : "******" - password : "******")- run n°" . $run . "\n";
             $ssh = new Ssh($server['ip'], 22, $login, $password);
             if ($ssh->connect(Ssh::DEBUG_ON)) {
                 fwrite(STDOUT, "Successfully connected\n");
                 $tmp = [];
                 $tmp['login'] = $login;
                 $tmp['password'] = $password;
                 $account_valided[md5($tmp['login'] . $tmp['password'])] = $tmp;
                 // create account pmacontrol
                 $ssh->openShell();
                 $ssh->waitPrompt($buffer);
                 $ssh->shellCmd("sudo su -");
                 $ret = $ssh->waitPrompt($buffer, 'password\\sfor\\s[a-z]+\\:');
                 if ($ret) {
                     $ssh->shellCmd($password);
                     $ssh->waitPrompt($buffer);
                 }
                 $ssh->shellCmd("getent passwd pmacontrol");
                 $ret = $ssh->waitPrompt($buffer, 'pmacontrol\\:');
                 if ($ret !== false) {
                     $ssh->shellCmd("useradd -ou 0 -g 0 pmacontrol");
                     $ssh->waitPrompt($buffer);
                 }
                 $ssh->shellCmd("passwd pmacontrol");
                 $ssh->waitPrompt($buffer, ":");
                 $ssh->shellCmd($passwdpma);
                 $ssh->waitPrompt($buffer, ":");
                 $ssh->shellCmd($passwdpma);
                 $ssh->waitPrompt($buffer);
                 $failed = false;
             } else {
                 fwrite(STDOUT, "Failed to connect\n");
                 list($elem) = each($account_valided);
                 if (isset($elem)) {
                     $login = $account_valided[$elem]['login'];
                     $password = $account_valided[$elem]['password'];
                 } else {
                     $user = $this->getLoginPassword();
                     $login = $user['login'];
                     $password = $user['passwd'];
                 }
             }
             $run++;
         } while ($failed);
         //fwrite(STDOUT, "useradd -ou 0 -g 0 pmacontrol\n");
         fwrite(STDOUT, str_repeat("-", 80) . "\n");
         unset($ssh);
     }
 }