簡體   English   中英

使用PHP通過SSH將新用戶和密碼添加到Debian

[英]Add new user and password to debian via SSH with PHP

我有問題要為通過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);
}
}
}
?>

似乎您正在尋找更改用戶密碼的非交互方式。

嘗試這個

echo "username:newpass"|chpasswd

這是chpasswd手冊頁。

此方法還適合為大量用戶更改密碼。

UPD:

要知道當前登錄的用戶名whoami可以使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM