解析"passwd"用phpseclib命令 [英] Parse "passwd" command with phpseclib

查看:93
本文介绍了解析"passwd"用phpseclib命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用phpseclib将"passwd"命令解析到我的机器上,但是无法通过第一个密码输入.看下面的代码.

I tried to parse "passwd" command to my machine with phpseclib but it can't go through the first password typing. Look at the code below.

$ssh->write("
    passwd $new_user\n
    $new_user_pw\n
    $new_user_pw\n 
"); 
$ssh->setTimeout(5);
echo $ssh->read('root@machine:~$');

我得到的输出是:

Last login: Tue Jun 17 12:23:01 2014 from 109.175.60.43 [root@machine ~]# [root@machine ~]# [root@machine ~]# passwd 8917f498 Changing password for user 8917f498. New password: 

或者我可以通过其他方式做到这一点吗?

Or can I do this with another way?

谢谢

推荐答案

如果您对自己执行passwd操作,则系统将提示您输入旧密码,然后再设置新密码.在我的Linux系统上,如果没有root用户,就无法对另一个用户执行passwd.这也可能会提示您输入密码.

If you do passwd on yourself you'll be prompted for the old pw before being allowed to set the new pw. And on my Linux system I wasn't able to do passwd on another user without doing root. That may result in you being for your prompted for your password as well.

作为一般规则,我发现提示的方法是通过对$ ssh-> setTimeout()进行空的$ ssh-> read()调用.例如.

As a general rule, the way I found out what the prompt is is by doing $ssh->setTimeout() with an empty $ssh->read() call. eg.

$ssh->enablePTY();
$ssh->exec('sudo passwd testuser');
$ssh->setTimeout(3);
echo $ssh->read();

输出Enter new UNIX password:.因此,我以此方式重写了程序:

That outputs Enter new UNIX password:. So I rewrite my program thusly:

$ssh->enablePTY();
$ssh->exec('sudo passwd testuser');
$ssh->setTimeout(3);
$ssh->read('password:');
$ssh->write("newpw\n");
echo $ssh->read();

输出Retype new UNIX password:.因此,我随后相应地修改了程序:

That outputs Retype new UNIX password:. So I then revise my program accordingly:

$ssh->enablePTY();
$ssh->exec('sudo passwd testuser');
$ssh->setTimeout(3);
$ssh->read('password:');
$ssh->write("newpw\n");
$ssh->read('password:');
$ssh->write("newpw\n");
echo $ssh->read();

输出passwd: password updated successfully.现在,我知道它可以工作了,我删除了$ ssh-> setTimeout(3)并更新了最后一个$ ssh-> read()来获取此信息:

That outputs passwd: password updated successfully. Now that I know it works I remove the $ssh->setTimeout(3) and update the last $ssh->read() to get this:

$ssh->enablePTY();
$ssh->exec('sudo passwd testuser');
$ssh->read('password:');
$ssh->write("newpw\n");
$ssh->read('password:');
$ssh->write("newpw\n");
$ssh->read('passwd:');

就是这样!它将像您的版本一样更改密码,而无需等待六秒钟.最后的$ ssh-> read()可能不是必需的.我会让你玩这个.

And that's it! It'll change the password without waiting for six seconds like your version does. That last $ssh->read() may not be necessary though. I'll let you play around with that.

这篇关于解析"passwd"用phpseclib命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆