SSH2更改用户密码 [英] SSH2 change a user password
问题描述
我一直在使用SSH,现在我需要通过PHP的ssh2更改用户的密码,
这里是我的代码:
$ stream = ssh2_exec($ ssh,'passwd test1234');
stream_set_blocking($ stream,true);
$ data ='';
while($ buffer = fread($ stream,4096)){
$ data。= $ buffer;
}
fclose($ stream);
echo $ data。< hr />;
$ stream = ssh2_exec($ ssh,'saulius123');
stream_set_blocking($ stream,true);
$ data ='';
while($ buffer = fread($ stream,4096)){
$ data。= $ buffer;
}
echo $ data。< hr />;
$ stream = ssh2_exec($ ssh,'saulius123');
stream_set_blocking($ stream,true);
$ data ='';
while($ buffer = fread($ stream,4096)){
$ data。= $ buffer;
}
echo $ data。< hr />;
但是这只是使我的PHP脚本挂起,任何想法?
ssh2_exec
调用命令;
也就是说, $ stream
可让您访问到标准输入和标准输出。因此,在尝试读回之前,您需要在 $ stream
上使用 fwrite
输出。
由于您已将数据流置于阻塞模式,因此 passwd
正在等待您的输入同时您的脚本正在等待 passwd
。因此,脚本会挂起。
I've been playing around with SSH and now I need to change a user's password via the PHP's ssh2,
Here's my code:
$stream = ssh2_exec($ssh, 'passwd test1234');
stream_set_blocking($stream, true);
$data = '';
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
fclose($stream);
echo $data."<hr/>";
$stream = ssh2_exec($ssh, 'saulius123');
stream_set_blocking($stream, true);
$data = '';
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
echo $data."<hr/>";
$stream = ssh2_exec($ssh, 'saulius123');
stream_set_blocking($stream, true);
$data = '';
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
echo $data."<hr/>";
However this just make's my PHP script hang, any ideas?
ssh2_exec
invokes the command; to send input, you'll need to write to the stream.
That is, $stream
gives you access to standard input and standard output. So you'll need to write the password you wish to set using fwrite
on $stream
before trying to read back the output.
Since you've put the stream in blocking mode, passwd
is awaiting your input (the password) at the same time your script is waiting for passwd
. As a result, the script hangs.
这篇关于SSH2更改用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!