简体   繁体   English

从远程服务器上运行的控制台应用程序捕获输出

[英]Capture output from console app running on remote server

I am running console app on remote server using WMI (C# code) 我正在使用WMI(C#代码)在远程服务器上运行控制台应用程序

I want to capture the output from the process. 我想捕获该过程的输出。

Can someone please help me in doing so? 有人可以帮我吗?

Scenario: 场景:

I have a webpage where user will provide server details. 我有一个网页,用户将在其中提供服务器详细信息。 When user clicks on submit, I want to call app on remote server and pass parameters. 当用户单击提交时,我想在远程服务器上调用应用并传递参数。 App on remote server will perform the action and will return back the output (output can be number, string or bool). 远程服务器上的App将执行该操作,并将返回输出(输出可以是数字,字符串或布尔值)。

Based on the output I need to prompt the user for more details or show error. 根据输出,我需要提示用户更多详细信息或显示错误。

object[] processToRun = new[] { "\"Path to exe\" \"Param1\" \"Param2\"" };
ConnectionOptions connection = new ConnectionOptions();
connection.Username = "username";
connection.Password = "password";
ManagementScope wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", "ServerName"), connection);
ManagementClass wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
wmiProcess.InvokeMethod("Create", processToRun);

I want to get the output of this. 我想得到这个输出。

Temporary Solution : But not working 临时解决方案:但不起作用

As a temporary solution, i added code to create a log file on a shared path which is accessible by everyone. 作为一个临时解决方案,我添加了代码以在每个人都可以访问的共享路径上创建日志文件。 But again here I am facing issue, when i call exe on remote server to write to the shared path, i always get error saying 'Access denied'. 但是在这里我再次遇到问题,当我在远程服务器上调用exe写入共享路径时,我总是收到错误消息,提示“访问被拒绝”。 But when i run the exe from the server itself, it works fine. 但是,当我从服务器本身运行exe时,它运行正常。

I've used PsExec in the past. 我过去曾经使用过PsExec This helped when a Firewall was blocking WMI requests. 当防火墙阻止WMI请求时,这会有所帮助。 You should be able to start PsExec just like starting any other process from C# and just redirect standard output. 您应该能够像从C#中启动任何其他进程一样启动PsExec,并且只需重定向标准输出即可。 I think you'll have some extra bytes and bits in the output stream from PsExec itself, but it should get you where you need to go after some trimming. 我认为您在PsExec本身的输出流中将有一些额外的字节和位,但是经过调整后,它应该可以使您到达需要的位置。

Assuming you want to collect the output from a second machine, one way forward is using sockets. 假设您要从第二台计算机上收集输出,向前一种方法是使用套接字。 Second machine will host a socket based listening service bound predefined port. 第二台机器将托管一个基于套接字的侦听服务绑定的预定义端口。

Once your console app has something to send back, it would open up a connection to the second machine and send the data. 一旦您的控制台应用程序有东西要发送回去,它将打开与第二台计算机的连接并发送数据。

If there are many clients and you are expecting frequent communication from them, you can go to a solution based on something like WCF or web services for which there are many off the shelf frameworks available 如果有很多客户,并且您希望与他们进行频繁的交流,则可以基于WCF或Web服务等解决方案,这些解决方案有很多现成的框架

Based on the addition info you provided , 根据您提供的附加信息

When you create the remote console process, pass it a unique id (GUID) which ties it to the client request originated at the web server. 创建远程控制台进程时,将其传递给其唯一ID(GUID),该ID将其与源自Web服务器的客户端请求相关联。 Your console app will now capture input and send it to the server via a socket along with the guid. 现在,您的控制台应用程序将捕获输入,并将其与GUID一起通过套接字发送到服务器。

The server can log each output into a database table using GUID to identify each session. 服务器可以使用GUID将每个输出记录到数据库表中,以标识每个会话。 Now the user who launch can query the output using the same GUID (remember the GUID is generated when the user create the request, so the user will have it always) and the server will get the output from the table and display 现在,启动用户可以使用相同的GUID查询输出(请记住,当用户创建请求时会生成GUID,因此用户将始终拥有它),服务器将从表中获取输出并显示

Additionally you can have a management task to clear output table of all results older than x number of days 此外,您可以执行一项管理任务来清除所有x天数之前的结果的输出表

Hope this helps 希望这可以帮助

暂无
暂无

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

相关问题 如何在静默运行时捕获dpinst的控制台输出? - How can I capture the console output of dpinst when running silently? 为什么我会收到无法从 Web 应用程序而不是控制台应用程序连接到远程服务器的异常? - Why do I get an Unable to connect to the remote server exception from a web app and not console app? 从控制台应用程序远程连接到RDC服务器需要引用什么库? - What library do I need to reference for remote connection to RDC server from console app? 如何捕获进程的输出(来自 OR 工具求解器)并通过控制台显示 - How to capture the output of a process (from OR Tools solver) and display it through console 如何从服务C#捕获控制台输出? - How to capture console output from a service C#? 如何:通过控制台从void方法捕获输出? - How to: capture output from void method through the console? 如何从本地控制台上用C#SSH.NET执行的远程主机(Raspberry Pi)上运行的Python程序连续写入输出? - How to continuously write output from Python Program running on a remote host (Raspberry Pi) executed with C# SSH.NET on local console? 捕获控制台输出以在VS中进行调试? - Capture console output for debugging in VS? 无法连接到远程服务器,但在fiddler运行metro应用程序时可以正常工作 - Unable to connect to the remote server but works when fiddler is running metro app 从 C# 应用程序运行.py 时的空白控制台 output - Blank console output when running .py from C# application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM