I'm trying to use SSH.NET to activate user accounts on my Linux Ubuntu VPS server.
I'm using the following code:
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();
}
}
When manually creating a user, I would run useradd username -s /bin/false
(to give the user no ssh access). Then I would run passwd username
.
This is where my error comes in. The application just freezes when running the passwd + password
command. I think it's because Linux asks you to enter the password and ssh.net thinks that it's waiting for a response. When I just run the first (useradd
) command, then the user is added and the program doesn't freeze. It just freezes with the second RunCommand
.
Is there any way to create user accounts in the way I'm trying to do this using SSH.Net?
Thanks!
Changing the double quotes to single quotes worked for me:
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();