how to connect to another host which has windows operating system by .net c#?

247 views Asked by At

I want to connect to a host which has windows operating system and then run some commands in that computer. generally I have try two solutions :

  1. I installed openSSh on both computers then used ssh command to connect to the host in CMD and it was successfull (this was done in order to become sure that I can connect) then I used SSH.net library to SSh to that host in Visual Studio but it didn't connect and the error was "an established connection was aborted by the software in your host machine " (I read all question about this error) . I think this error comes because ssh.net library is written for linux connections and not windows. am I right?

  2. I read about WMI (windows management instrumentation) and try to create connection first by windows PowerShell (again to become sure that there isn't any problem). I have done many actions to solve the problem "accessing deny" (like check firewall, security access and permission, run some commands in windows power shell) but it hasn't solved yet.(I almost read all question about this problem). I have try this code in my application but exception comes.

    ip = "172.17.9.2";
    username = "****";
    password = "***";
    port = 22 ;
    var processToRun = new[] { "notepad.exe" };
    var connection = new ConnectionOptions();
    connection.Username = username;
    connection.Password = password;
    var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", "FB"), connection);
    var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
        wmiProcess.InvokeMethod("Create", processToRun);
    

I spend a lot of time on WMI to understand what problem is. Now I'm looking for another way which is simple and easy to implement.

1

There are 1 answers

0
BugFinder On BEST ANSWER

When using WMI you need to use a user account which as privileges on the remote machine

so, if you have PC1, and you log in as user1, and you try to login to PC2, its trying to log in as PC1\user1.. So you need to be specific about the username you login to the remote machine, or ensure that the user on the second machine.

You may need to send PC2\User1 to login on the remote machine as User1 on there as to it, its a whole different person