【发布时间】:2017-02-17 22:32:45
【问题描述】:
我无法将通过 ssh 和 php 生成的新用户的密码输入到 Debian 服务器。 要添加新用户,我只需简单地编写“useradd $username”。但是,如果我想为该用户设置密码,它应该类似于“passwd $username”,然后我应该插入密码。但是如何在 PHP 中做到这一点呢?
这是我的完整代码:
<?php
$username = "abcd";
$password = "456789";
$expired = "5";
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("128.199.xxx.xx", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "xxxxxx")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
if (!($stream = ssh2_exec($con, "passwd 'haha'"))){
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
echo "Username berhasil dibuat";
echo $data;
fclose($stream);
}
}
}
?>
【问题讨论】: