0

我正在尝试使用 SSH.NET 在我的 Linux Ubuntu VPS 服务器上激活用户帐户。

我正在使用以下代码:

public void CreateUser()
{
    var connectionInfo = new Renci.SshNet.PasswordConnectionInfo("serverip", "root", "serverpassword");   
    using (var ssh = new Renci.SshNet.SshClient(connectionInfo))
    {
        string username = tbUser.Text;
        string password = tbPass.Text;
        ssh.Connect();
        var command = ssh.RunCommand("useradd " + username + " -s /bin/false");
        command.Execute();
        command = ssh.RunCommand("passwd " + password);
        command.Execute();
        command = ssh.RunCommand(password);
        command.Execute();
        command = ssh.RunCommand(password);
        command.Execute();
        ssh.Disconnect();
    }
}

手动创建用户时,我会运行useradd username -s /bin/false(不给用户 ssh 访问权限)。然后我会跑passwd username

这就是我的错误所在。应用程序在运行passwd + password命令时会冻结。我认为这是因为 Linux 要求您输入密码,而 ssh.net 认为它正在等待响应。当我只运行第一个 ( useradd) 命令时,会添加用户并且程序不会冻结。它只是与第二个冻结RunCommand

有没有办法以我尝试使用 SSH.Net 的方式创建用户帐户?

谢谢!

4

1 回答 1

0

将双引号更改为单引号对我有用:

ssh.Connect();

var command = ssh.CreateCommand("useradd " + username + " -s /bin/false");
command.Execute();

command = ssh.CreateCommand("echo '" + username + ":" + password + "' >> create.txt");
command.Execute();
command = ssh.CreateCommand("chpasswd < create.txt");
command.Execute();
command = ssh.CreateCommand("rm create.txt");
command.Execute();

ssh.Disconnect();
于 2014-08-19T17:29:19.373 回答