简体   繁体   English

如何通过.net c#连接到另一台具有Windows操作系统的主机?

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

I want to connect to a host which has windows operating system and then run some commands in that computer. 我想连接到具有Windows操作系统的主机,然后在该计算机上运行一些命令。 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) . 我在两台计算机上都安装了openSSh ,然后使用ssh命令连接到CMD中的主机,并且成功完成了(这样做是为了确保可以连接),然后我使用SSH.net库将SSh连接到Visual Studio中的该主机。但它没有连接,并且错误是“主机计算机中的软件中止了已建立的连接” (我阅读了有关此错误的所有问题)。 I think this error comes because ssh.net library is written for linux connections and not windows. 我认为出现此错误是因为ssh.net库是为Linux连接而不是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). 我阅读了有关WMI (Windows管理规范)的信息,并尝试首先通过Windows PowerShell创建连接(再次确保没有任何问题)。 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). 我已经采取了许多措施来解决“拒绝访问”问题(例如检查防火墙,安全访问和权限,在Windows Power Shell中运行一些命令),但尚未解决。(我几乎阅读了有关此问题的所有问题)。 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. 我在WMI上花费了大量时间来了解问题所在。 Now I'm looking for another way which is simple and easy to implement. 现在,我正在寻找另一种简单且易于实现的方法。

When using WMI you need to use a user account which as privileges on the remote machine 使用WMI时,您需要使用作为远程计算机特权的用户帐户

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. 因此,如果您拥有PC1,并且您以user1身份登录,然后尝试登录PC2,则尝试以PC1 \\ user1身份登录。.因此,您需要具体说明登录到远程计算机的用户名,或者确保用户在第二台机器上。

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 您可能需要发送PC2 \\ User1才能以其上的User1身份在远程计算机上登录,这是一个完全不同的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM