0
Debianサーバにssh経由でphpで生成された新しいユーザのパスワードを入力する際に問題があります。 新しいユーザーを追加するにはsimpyだけで "useradd $ username"と書いてください。しかし、そのユーザーのパスワードを設定する場合は、 "passwd $ username"のようにして、パスワードを挿入する必要があります。しかし、PHPでこれを行うには?PHPでSSH経由でDebianに新しいユーザとパスワードを追加
これは私の完全なコードです:
<?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);
}
}
}
?>
申し訳ありませんが、まだ理解していませんでした。このコードに変更することはできますか? '($ stream = ssh2_exec($ con、' "username:newpass" | chpasswd '))){' @Paul – tzudin
あなたが望むものを実現するには、現在アクティブなユーザー名を代用する必要があります。この目的のために、私が答えに加えた 'whoami'を使うことができます。まず、現在のユーザ名を読み、 ':'の前に置き換え、次に 'newpass'の代わりに新しいパスワードをプレーンテキストとして入力します。 –