0

我正在尝试更改密码。首先我必须以 root 身份登录,但我不知道确切的密码,所以我需要尝试 3 个不同的密码。我找到了如下解决方案,但我无法进入 if 语句。如果第一个密码不正确,我想尝试另一个。如果条件为空,我就离开了,因为我尝试了几件事,但都没有奏效。

if (!$ssh->login('ntsysadm', 'ABCD123')) {
    echo" Login Failed to server". $iplist[$i];
}else{
    echo "<br>";
    echo "Connected to ".$iplist[$i];
    echo "<br>";
    $ssh->write("su - root\n");
    $ssh->read('Password:');
    $ssh->write("ABCD123\n");
    if (  ) {
        $ssh->write("su - root\n");
        $ssh->read('Password:');
        $ssh->write("ABC123\n");
        if (  ) {
            $ssh->write("su - root\n");
            $ssh->read('Password:');
            $ssh->write("ABC1234\n");} }
            $ssh->read('#');
            $ssh->write("passwd ".$_POST['username']."\n");
            $ssh->read('password:');
            $ssh->write($_POST['newpassword']."\n");
            $ssh->read('password:');
            $ssh->write($_POST['newpassword']."\n");
            echo $logDetail = $ssh->read('successfully.');
            echo "<br>";
            mysql_query("INSERT INTO log VALUES ('','".$_SESSION['fullname']."','".$idlabuser."','".$serverId."','".$command."','".$datetime."','".$logDetail."') ");
    }
4

1 回答 1

0
$passwords = array('pass1', 'pass2', 'pass3');

if (!$ssh->login('ntsysadm', 'ABCD123')) {
   echo "Login failed.";
   return;
}
//No need to use else
foreach($pass in $passwords) {
   $ssh->write("su root \n");
   $ssh->write(sprintf("%s \n", $pass);
   if(strstr($ssh->HowIsThisFunctionCalled, 'Logged in'))
      echo sprintf('Password %s is correct.', $pass);
    else
      echo sprintf('Password %s is not correct.', $pass);
}

将“HowIsThisFunctionCalled”替换为读取整个终端文本或最后一条消息的函数,很确定这存在于 PHPSECLIB 中。

于 2013-08-22T09:23:30.810 回答