Exemple #1
0
            // We prepare and we send the command into the screen
            $cmd = "screen -S " . $session . " -p 0 -X stuff \"" . $cmdRcon . "\"`echo -ne '\r'`";
            $ssh->exec($cmd . "\n");
            unset($cmd);
            // Adding event to the database
            $message = 'RCON command (' . mysql_real_escape_string($cmdRcon) . ') sent to : ' . mysql_real_escape_string($server['name']);
            query_basic("INSERT INTO `" . DBPREFIX . "log` SET `serverid` = '" . $serverid . "', `message` = '" . $message . "', `name` = '" . mysql_real_escape_string($_SESSION['adminfirstname']) . " " . mysql_real_escape_string($_SESSION['adminlastname']) . "', `ip` = '" . $_SERVER['REMOTE_ADDR'] . "'");
            unset($cmdRcon);
            header('Location: utilitiesrcontool.php?serverid=' . urlencode($serverid));
            die;
        }
        // We retrieve screen contents
        $ssh->write("screen -R " . $session . "\n");
        $ssh->setTimeout(1);
        @$ansi->appendString($ssh->read());
        $screenContents = htmlspecialchars_decode(strip_tags($ansi->getScreen()));
        $ssh->disconnect();
        unset($session);
        include "./bootstrap/header.php";
        /**
         * Notifications
         */
        include "./bootstrap/notifications.php";
        ?>
			<script type="text/javascript">
			$(document).ready(function() {
				prettyPrint();
			});
			</script>
			<div class="page-header">
				<h1><small><?php 
 protected final function RemoteExec($cmd, $rand = null, $method = null)
 {
     if (($result = $this->InitSSH()) !== true) {
         $this->SessionSwap('previous');
         return $result;
         //trigger_error($result);
     }
     if (strstr($cmd, 'sudo ')) {
         exit('ERROR: RemoteExec() remove sudo in commands, we are already root');
     }
     if (!$this->sudoOK) {
         list($user, $pass) = explode(' ', $this->Decrypt($_SESSION['cred']));
         $this->GoSUDO($pass);
     }
     $is_ansi = ($cmd and in_array($cmd, array('top')) and $this->sudoOK) ? 1 : 0;
     $polling = ($rand and ctype_alnum($rand) and in_array($method, array('nohup', 'pscreen'))) ? 1 : 0;
     if ($polling) {
         // no need to redirect stderr to stdout, nohup does this for you
         //$randcapture = ' >/tmp/' . $randfile . ' &';
         // bashpre-4 more compatible
         //$randcapture = ' >/tmp/' . $randfile . ' 2>&1 &';
         // bash4 is the new way, but less compatible
         //$randcapture = ' &>/tmp/' . $randfile;
         $log = '/tmp/' . $this->tmp_prepend . $rand;
         $cnf = '/root/.screenrc_panel';
         if ($method == 'nohup') {
             $this->ssh->write("touch {$log}; nohup {$cmd} >{$log} &\n");
             $out = $this->SSHRead($_SESSION['rprompt'], 0, 1);
             list(, $pid) = explode(' ', $out);
             if ($pid and is_numeric($pid)) {
                 $this->ssh->write("echo '{$pid}' > {$log}.pid\n");
             } else {
                 exit('PanelCommon->RemoteExec(): method: nohup, non-numeric pid: ' . $pid);
             }
             $this->SSHRead($_SESSION['rprompt']);
             return $pid;
         } else {
             if ($method == 'pscreen') {
                 $this->ssh->write("screen -wipe; screen -li\n");
                 $out = $this->SSHRead($_SESSION['rprompt'], 0, 1);
                 if (stristr($out, 'no sockets found')) {
                     $write_cnf = "echo 'log on' > {$cnf}; echo 'logfile {$log}' >> {$cnf}; echo 'logfile flush 5' >> {$cnf}";
                     $this->ssh->write("{$write_cnf}; screen -c {$cnf} -dmLS panel\n");
                     $out = $this->SSHRead($_SESSION['rprompt']);
                     $this->ssh->write("screen -li | grep panel | cut -d'.' -f1\n");
                     $pid = $this->SSHRead($_SESSION['rprompt'], 0, 1);
                     if ($pid and is_numeric($pid)) {
                         $this->ssh->write("echo '{$pid}' > {$log}.pid\n");
                     } else {
                         exit('PanelCommon->RemoteExec(): method: pscreen, non-numeric pid: ' . $pid);
                     }
                     $this->SSHRead($_SESSION['rprompt']);
                     $_SESSION['pscreen'] = $_SESSION['pscreen2'] = $rand;
                     return $pid;
                 } else {
                     // get pid of running screen named 'panel'
                     // $screenPID = 'screen -li | grep panel | sed -e "s/^[ \t]*//" | cut -d"." -f1';
                     // get config file of screen with specified pid
                     // $screenCONF = 'ps aux | grep screen | grep $('.$screenPID.') | tr -s " " | cut -d" " -f13';
                     // get $rand from logfile
                     // $screenRAND = 'cat $('.$screenCONF.') | grep "logfile /" | cut -d"/" -f3 | | cut -d"_" -f2';
                     // below is on long command to do the above action, you have to use bash vars, you cannot nest $() like you would think
                     $cmd_get_rand = 'pid=$(screen -li | grep panel | sed -e "s/^[ \\t]*//" | cut -d"." -f1); conf=$(ps aux | grep screen | grep $pid | tr -s " " | cut -d" " -f13); echo $(cat $conf | grep "logfile /" | cut -d"/" -f3  | cut -d"_" -f2)';
                     //if (! $_SESSION['pscreen'] AND $_SESSION['pscreen2']) $_SESSION['pscreen'] = $_SESSION['pscreen2'];
                     //else if (! $_SESSION['pscreen2'])
                     //{
                     //$this->ssh->write("cat {$c} | grep 'logfile /' | cut -d'/' -f3 | | cut -d'_' -f2\n");
                     $this->ssh->write($cmd_get_rand . "\n");
                     $out = $this->SSHRead($_SESSION['rprompt'], 0, 1);
                     //exit($out);
                     $_SESSION['pscreen'] = $_SESSION['pscreen2'] = $out;
                     //}
                     $cmd1 = $cmd2 = '';
                     if (strstr($cmd, '----')) {
                         list($cmd1, $cmd2) = explode('----', $cmd, 2);
                     }
                     $cmd_do_rtn = 'screen -S panel -p 0 -X stuff "' . $cmd . '$(echo -ne \'\\015\')"';
                     $cmd_no_rtn = 'screen -S panel -p 0 -X stuff "' . $cmd . '"';
                     $cmd = $cmd2 === 'noenterkey' ? $cmd_no_rtn : $cmd_do_rtn;
                     $this->ssh->write($cmd . "\n");
                     $this->SSHRead($_SESSION['rprompt']);
                     return 'send: ' . $cmd;
                 }
             }
         }
     } else {
         if (strstr($cmd, 'ls ')) {
             $this->ssh->write("{$cmd}{$this->filter_colors}\n");
         } else {
             $this->ssh->write("{$cmd}\n");
         }
         if ($method === 'pscreen') {
             $this->ssh->write("echo -n '{$_SESSION['pscreen']}'\n");
             $out = $this->SSHRead($_SESSION['pscreen'] . $_SESSION['rprompt'], 0, 1);
             $out = $this->MakeSingleSpace($out);
             $out = str_replace("{$_SESSION['rprompt']} echo -n '{$_SESSION['pscreen']}'", '', $out);
             return trim($out);
         }
     }
     $out = $this->SSHRead($_SESSION['rprompt']);
     if ($is_ansi) {
         $ansi = new File_ANSI();
         if ($cmd === 'top') {
             $this->ssh->write('u');
             $this->ssh->write("{$user}\n");
             $out = $this->SSHRead($_SESSION['rprompt']);
             $ansi->appendString(trim($out));
             $out = htmlspecialchars_decode(strip_tags($ansi->getScreen()));
             $this->ssh->write('q');
             $this->SSHRead($_SESSION['rprompt']);
         }
         return $this->SSHClean($out);
     } else {
         $out = $this->SSHClean($out);
         return $out;
     }
 }