我试图使用SSH.NET激活我的服务器上的用户帐户。
我使用以下代码:
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的方式创建用户帐户吗?
谢谢!
发布于 2014-08-19 17:29:19
将双引号改为单引号对我有效:
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();https://stackoverflow.com/questions/25388154
复制相似问题