Exemple #1
0
 /**
  */
 protected function _changePassword($user, $oldpass, $newpass)
 {
     if (!Horde_Util::loadExtension('expect')) {
         throw new Passwd_Exception(_("expect extension cannot be loaded"));
     }
     // Set up parameters
     foreach (array('logfile', 'loguser', 'timeout') as $val) {
         if (isset($this->_params[$val])) {
             ini_set('expect.' . $val, $this->_params[$val]);
         }
     }
     // Open connection
     $call = sprintf('ssh %s@%s %s', $user, $this->_params['host'], $this->_params['program']);
     if (!($this->_stream = expect_popen($call))) {
         throw new Passwd_Exception(_("Unable to open expect stream"));
     }
     // Log in
     $this->_ctl('(P|p)assword.*', _("Could not login to system (no password prompt)"));
     // Send login password
     fwrite($this->_stream, $oldpass . "\n");
     // Expect old password prompt
     $this->_ctl('((O|o)ld|login|current).* (P|p)assword.*', _("Could not start passwd program (no old password prompt)"));
     // Send old password
     fwrite($this->_stream, $oldpass . "\n");
     // Expect new password prompt
     $this->_ctl('(N|n)ew.* (P|p)assword.*', _("Could not change password (bad old password?)"));
     // Send new password
     fwrite($this->_stream, $newpass . "\n");
     // Expect reenter password prompt
     $this->_ctl("((R|r)e-*enter.*(P|p)assword|Retype new( UNIX)? password|(V|v)erification|(V|v)erify|(A|a)gain).*", _("New password not valid (too short, bad password, too similar, ...)"));
     // Send new password
     fwrite($this->_stream, $newpass . "\n");
     // Expect successfully message
     $this->_ctl('((P|p)assword.* changed|successfully)', _("Could not change password."));
 }
Exemple #2
0
 /**
  * Local shell wrapper
  * @throws \Exception
  */
 public function __construct()
 {
     $cur_shell = trim(shell_exec('echo $0'));
     if (!in_array($cur_shell, array('sh', 'bash'))) {
         throw new \Exception("Unknown shell");
     }
     $this->__stream = expect_popen($cur_shell);
 }
Exemple #3
0
function hostHardCtl($op, $pass)
{
    openlog("myScriptLog", LOG_PID | LOG_PERROR, LOG_LOCAL0);
    syslog(LOG_WARNING, "Attempting to exec hostHardCtl");
    switch ($op) {
        case 0:
            $cmd = '"/sbin/shutdown -r now"';
            break;
        case 1:
            $cmd = '"/sbin/shutdown -h now"';
            break;
        default:
            syslog(LOG_WARNING, "unknown command");
            break;
    }
    //$cmd = '"ls -la /root"';
    $cases = array(array(0 => "Password: "******"PASSWORD"));
    $full_cmd = "su -c " . $cmd;
    syslog(LOG_WARNING, "execute: " . $full_cmd);
    $stream = expect_popen($full_cmd);
    $ret = expect_expectl($stream, $cases);
    switch ($ret) {
        case "PASSWORD":
            fwrite($stream, $pass . "\n");
            syslog(LOG_WARNING, "pwd: " . $pass);
            break;
        case EXP_TIMEOUT:
            syslog(LOG_WARNING, "EXP_TIMEOUT");
            return false;
            break;
        case EXP_EOF:
            syslog(LOG_WARNING, "EXP_EOF");
            return false;
            break;
        default:
            syslog(LOG_WARNING, "EXP_TIMEOUT");
            return false;
            break;
    }
    $out = '';
    while ($line = fgets($stream)) {
        $out .= $line;
        syslog(LOG_WARNING, "ret: " . $line);
    }
    fclose($stream);
    closelog();
    return $out;
}
Exemple #4
0
 public function checkRepExist($repName)
 {
     $logfile = "/home/apacheUser/temp4autotestsys/" . time() . rand(11, 99) . ".log";
     ini_set("expect.timeout", 3);
     ini_set("expect.loguser", "Off");
     ini_set("expect.logfile", $logfile);
     $cases = array(array(0 => "yes/no", 1 => "YESNO", EXP_EXACT), array(0 => "10.18's password:"******"PASSWORD", EXP_EXACT), array(0 => "\$", 1 => "EXPECTSHELL", EXP_EXACT));
     $stream = expect_popen("ssh www@*.*.*.18");
     $i = 0;
     $result = "";
     while (true) {
         switch (expect_expectl($stream, $cases, $match)) {
             case "YESNO":
                 fwrite($stream, "yes\r");
                 break;
             case "PASSWORD":
                 fwrite($stream, "www\r");
                 break;
             case "EXPECTSHELL":
                 if ($i == 1) {
                     break 2;
                 } else {
                     fwrite($stream, "mkdir /data1/www/*/{$repName}\r");
                     fwrite($stream, "mkdir /data1/www/*/{$repName}\r");
                     fwrite($stream, "mkdir /data1/www/*/cloud/{$repName}\r");
                     $i = $i + 1;
                     break;
                 }
             case EXP_TIMEOUT:
             case EXP_EOF:
                 break 2;
             default:
                 break 2;
         }
     }
     fclose($stream);
     return $logfile;
     //        $result = exec("sudo /usr/bin/expect ../app/script/checkRepExist ".$repName,$result1);
     //        $isExist = false;
     //        foreach($result1 as $item)
     //            if(strstr($item,"File exists")){
     //                $isExist=true;
     //                break;
     //            };
     //        return $isExist;
 }
Exemple #5
0
function se($c)
{
    @ob_start();
    if ($r = @`echo 1`) {
        echo @`{$c}`;
    } elseif (@function_exists('exec')) {
        @exec($c, $r);
        echo @implode("\n", $r);
    } elseif (@function_exists('system')) {
        @system($c);
    } elseif (@function_exists('shell_exec')) {
        echo @shell_exec($c);
    } elseif (@function_exists('passthru')) {
        @passthru($c);
    } elseif (@is_resource($f = @popen($c, 'r'))) {
        while (!feof($f)) {
            echo fread($f, 1024);
        }
        @pclose($f);
    } elseif (@is_resource($f = @proc_open($c, array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'a')), $p))) {
        echo @stream_get_contents($p[1]);
        @proc_close($f);
    } elseif (@function_exists('pcntl_exec')) {
        @pcntl_exec('/bin/sh', array('-c', $c));
    } elseif (@function_exists('expect_popen') && is_resource($f = @expect_popen($c))) {
        while (!feof($f)) {
            echo fread($f, 1024);
        }
        @fclose($f);
    } elseif (@is_resource($f = @fopen('expect://' . $c, 'r'))) {
        while (!feof($f)) {
            echo fread($f, 1024);
        }
        @fclose($f);
    }
    echo escHTML(@ob_get_clean());
}
Exemple #6
0
 public function getSTokenOnline()
 {
     $mobile = Input::get('mobile');
     $logfile = "/home/apacheUser/temp4autotestsys/" . time() . rand(11, 99) . ".log";
     ini_set("expect.timeout", 3);
     ini_set("expect.loguser", "Off");
     ini_set("expect.logfile", $logfile);
     $cases = array(array(0 => "yes/no", 1 => "YESNO", EXP_EXACT), array(0 => "'s password:"******"PASSWORD", EXP_EXACT), array(0 => "bash-4.1\$", 1 => "EXPECTSHELL", EXP_EXACT), array(0 => "选择服务器:", 1 => "SERVER", EXP_EXACT), array(0 => "请选择账号:", 1 => "ZHANGHAO", EXP_EXACT), array(0 => "mysql>", 1 => "MYSQL", EXP_EXACT));
     $stream = expect_popen("ssh *@1*");
     while (true) {
         switch (expect_expectl($stream, $cases)) {
             case "YESNO":
                 fwrite($stream, "yes\r");
                 break;
             case "PASSWORD":
                 fwrite($stream, "*\r");
                 break;
             case "SERVER":
                 fwrite($stream, "0\n");
                 usleep(300000);
                 fwrite($stream, "\r");
                 usleep(100000);
                 break;
             case "ZHANGHAO":
                 fwrite($stream, "0\r");
                 break;
             case "EXPECTSHELL":
                 fwrite($stream, "mysql -u*@l1 *P*\r");
                 break 2;
             case EXP_TIMEOUT:
             case EXP_EOF:
                 break 2;
             default:
                 break 2;
         }
     }
     $time = 0;
     while (true) {
         switch (expect_expectl($stream, $cases)) {
             case "EXPECTSHELL":
                 fwrite($stream, "mysql*@l1 -h 1*\r");
                 break;
             case "MYSQL":
                 if ($time < 2) {
                     fwrite($stream, "use user_db;\r");
                     fwrite($stream, "select sToken from t_user_login_status where iUserID=(select iAutoID from\r\n        t_user where sMobile= " . $mobile . ") order by iAutoID desc limit 1;\r");
                     $time = $time + 1;
                 }
                 break;
             case EXP_TIMEOUT:
             case EXP_EOF:
                 break 2;
             default:
                 break 2;
         }
     }
     fclose($stream);
     return $logfile;
 }