首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSH.Net创建用户帐户

SSH.Net创建用户帐户
EN

Stack Overflow用户
提问于 2014-08-19 16:09:11
回答 1查看 1.2K关注 0票数 0

我试图使用SSH.NET激活我的服务器上的用户帐户。

我使用以下代码:

代码语言:javascript
复制
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的方式创建用户帐户吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-19 17:29:19

将双引号改为单引号对我有效:

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25388154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档