我有问题为通过ssh生成的新用户输入密码到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);
}
}
}
?>